当前位置: 首页 > news >正文

CD49.【C++ Dev】容器适配器模式

目录

容器适配器模式

一个类实现数组栈和链式栈

拓展:带容器适配器的迭代器函数的写法

用vector实现队列queue

方法1:强制适配

方法2:使用deque适配


容器适配器模式

容器适配器模式是结构设计模式的一种,看看STL的栈和队列:

1.发现它们第二个模版参数都是class Container = deque<T>,使用了缺省参数

2.新名词:container adaptor(容器适配器)

Stacks are a type of container adaptor.

Queues are a type of container adaptor.

栈和队列是容器适配器的一种,它们是通过其他容器转换而来的,也可以说是复用.

下面从一个类实现数组栈和链式栈为例说明容器适配器:

一个类实现数组栈和链式栈

数组栈:即底层是用数组实现的;链式栈:即底层是用链表实现的

框架:

#pragma once
#include <list>
#include <vector>//动态数组
#include <array>//静态数组
namespace mystl
{template<class T, class Container>class  stack{public://成员函数private:Container _con;}
}

Container可以为vector<int>,也可以是list<int>

成员函数中可以直接调用vector或list的成员函数,因==因为它们很多接口是统一的:

统一的调用代码:

_con.容器的成员函数(参数);

完整实现: 

namespace mystl
{template<class T, class Container>class  stack{public:void push(const T& x){_con.push_back(x);}void pop(){_con.pop_back();}T& top(){return _con.back();}size_t size(){return _con.size();}bool empty(){return _con.empty();}size_t max_size(){return _con.max_size();}private:Container _con;};
}

当然也可以用缺省参数:

template<class T, class Container=std::vector<T>>
class  stack
{//......
}

这样可以省略第二个参数: mystl::stack<int> st1,默认是用vector实现的

 注:不用写构造函数和析构函数,因为Container _con是自定义类型,编译器会自动调用它的构造函数和析构函数


拓展:带容器适配器的迭代器函数的写法

虽然栈不需要迭代器,但如果加上vector或list的迭代器的函数应该怎么写?

以begin()为例,如果直接这样写会报错:

iterator begin()
{return _con.begin();
}

上面这样写是有歧义的,当模版没有实例化时,编译器不确定iterator是类型还是静态成员变量,因此要使用typename关键字来告知编译器iterator是类型

typename typedef Container::iterator iterator;

也可以看看IBM公司的参考手册对此的描述:

 限定名(qualified name)指代某个类型且这个名字依赖于模板参数时,要使用关键字 typename 


提交到LeetCode上https://leetcode.cn/problems/implement-stack-using-queues/description/测试:

数组栈:

namespace mystl
{template<class T, class Container>class  stack{public:void push(const T& x){_con.push_back(x);}void pop(){_con.pop_back();}T& top(){return _con.back();}size_t size(){return _con.size();}bool empty(){return _con.empty();}size_t max_size(){return _con.max_size();}private:Container _con;};
}class MyStack {
public:MyStack() {}void push(int x) {st1.push(x);   }int pop() {int ret=st1.top();st1.pop();return ret;   }int top() {return st1.top(); }bool empty() {return st1.empty();}mystl::stack<int, std::vector<int>> st1;
};

提交结果:

链式栈(将上方代码的mystl::stack<int, std::vector<int>> st1;的vector改成list)的提交结果:

用vector实现队列queue

之前在98.【C语言】数据结构之队列文章提到过用数组实现queue

方法1:强制适配

除了pop()需要强制适配,其余的成员函数比较好写:

namespace mystl
{template<class T, class Container=std::vector<T>>class queue{public:void push(T& x){_con.push_back(x);}T& front(){return _con.front();}T& back(){return _con.back();}size_t size(){return _con.size();}bool empty(){return _con.empty();}private:Container _con;};
}

pop()的写法:

弹出队头元素,在vector数组中就是删除下标为0(当然可以传begin()迭代器)的元素 

void pop()
{_con.erase(_con.begin());
}

(注:vector没有pop_front函数) 

 提交到LeetCode的https://leetcode.cn/problems/implement-queue-using-stacks/description/上测试:

namespace mystl
{template<class T, class Container=std::vector<T>>class queue{public:void pop(){_con.erase(_con.begin());}void push(T& x){_con.push_back(x);}T& front(){return _con.front();}T& back(){return _con.back();}size_t size(){return _con.size();}bool empty(){return _con.empty();}private:Container _con;};
}class MyQueue {
public:MyQueue() {}void push(int x) {q.push(x);    }int pop() {int tmp=q.front();q.pop();return tmp;}int peek() {return  q.front(); }bool empty() {return q.empty(); }mystl::queue<int> q;
};

提交结果:

 

方法1的实现也能适配list容器,提交结果:

但方法1的缺点是:vector的头删太慢了,降低效率

方法2:使用deque适配

看看STL库中queue的pop()写法:

#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
template <class T, class Sequence>
#endif
class queue {friend bool operator== __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);friend bool operator< __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
public:typedef typename Sequence::value_type value_type;typedef typename Sequence::size_type size_type;typedef typename Sequence::reference reference;typedef typename Sequence::const_reference const_reference;
protected:Sequence c;
public:bool empty() const { return c.empty(); }size_type size() const { return c.size(); }reference front() { return c.front(); }const_reference front() const { return c.front(); }reference back() { return c.back(); }const_reference back() const { return c.back(); }void push(const value_type& x) { c.push_back(x); }void pop() { c.pop_front(); }
};

会发现默认的适配器为deque,而且pop()直接调用pop_front(),代码简洁,可读性高,并没有强制适配

稍微改改,提交到Leetcode的题上是能过的

namespace mystl
{template <class T, class Sequence = std::deque<T>>class queue {public:typedef typename Sequence::value_type value_type;typedef typename Sequence::size_type size_type;typedef typename Sequence::reference reference;typedef typename Sequence::const_reference const_reference;protected:Sequence c;public:bool empty() const { return c.empty(); }size_type size() const { return c.size(); }reference front() { return c.front(); }const_reference front() const { return c.front(); }reference back() { return c.back(); }const_reference back() const { return c.back(); }void push(const value_type& x) { c.push_back(x); }void pop() { c.pop_front(); }};
}class MyQueue {
public:MyQueue() {}void push(int x) {q.push(x);    }int pop() {int tmp=q.front();q.pop();return tmp;}int peek() {return  q.front(); }bool empty() {return q.empty(); }mystl::queue<int> q;
};

提交结果:

 

下一篇将讲deque的设计思路

http://www.dtcms.com/a/277448.html

相关文章:

  • 深入解析5G核心网容灾:UDM 故障场景下 SMF 容灾机制深度解析
  • C++ 单例模式实现
  • 【读书笔记】《C++ Software Design》第五章:The Strategy and Command Design Patterns
  • Java学习------设计模式(1)
  • ZKmall开源商城技术攻略:轻松掌握规则引擎与Spring Boot3接口的开发技巧
  • Linux V4L2应用编程常用结构体介绍
  • STEP 7-Micro/WIN SMART 编程软件:从入门到精通的使用指南
  • 面试150 从前序与中序遍历构造二叉树
  • STM32-第五节-TIM定时器-1(定时器中断)
  • Clojure和Golang中的Channel有什么异同(TBC)
  • 构建应用内智能:衡石嵌入式BI如何打造“指标中台”驱动的场景化分析
  • Python文件路径操作全面指南:从基础到高级应用
  • 深入理解数据库连接池:原理、实现与Druid实战
  • MCU中的系统控制器(System Controller)是什么?
  • Spring Boot + MyBatis 实现用户登录功能详解(基础)
  • PaperPel
  • Oracle SQL - 使用行转列PIVOT减少表重复扫描(实例)
  • AI驱动的软件工程(上):人机协同的设计与建模
  • 【读书笔记】《C++ Software Design》第六章深入剖析 Adapter、Observer 和 CRTP 模式
  • 实现“micro 关键字搜索全覆盖商品”并通过 API 接口提供实时数据(一个方法)
  • fatal: active `post-checkout` hook found during `git clone`
  • mapstruct与lombok冲突原因及解决方案
  • 【Linux 学习指南】网络基础概念(一):从协议到分层,看透计算机通信的底层逻辑
  • LeetCode|Day9|976. 三角形的最大周长|Python刷题笔记
  • 通过反射,提取 Cat 类 泛型 父类 接口 属性 的具体类型参数
  • 【一起来学AI大模型】部署优化推理加速:TensorRT-LLM
  • 华为交换机 undo negotiation auto功能(华为交换机端口接光纤两端起不来)
  • Jvm优化高手-笔记
  • Cursor精准上下文指定
  • 印度纱丽变革:传统靛蓝工艺在无性别斗篷中的延续