到底什么是“容器适配器”?

首先,我们要明白适配器是干什么的?其实就是一个接口转换装置,是得我们能用特定的方法去操作一些我们本来无法操作的东西。举一个例子,比如你的一个设备支持串口线,而你的电脑支持的是usb口,这时候,我们没有必要重新买一个支持usb的设备,只需要一根串口转usb口的小玩意,让你的设备能够连接到usb插口上,而它就是适配器。

容器适配器到底是什么意思?

不同场景下,由于不同的序列式容器其底层采用的数据结构不同,因此容器适配器的执行效率也不尽相同。但通常情况下,使用默认的基础容器即可。当然,我们也可以手动修改,具体的修改容器适配器基础容器的方法,请详看下面自定义Stack类演示:

以Stack为例,容器适配器的意思就是底层使用的容器不同,以Stack使用vector数组为例:

Person.h

#pragma once  
  
#include <iostream>  
#include <sstream>  
#include <string>  
using namespace std;  
  
class Person  
{  
private:  
    string name;  
    int age;  
public:  
    Person() = default;  
    Person(int age, string name);  
    Person(const Person& obj);  
    Person(Person&& obj);  
  
    Person& operator = (Person obj);  
    friend ostream& operator << (ostream& cout, Person& obj);  
  
    ~Person() = default;  
};  

Person.cpp

#include "Person.h"  
  
Person::Person(int age, string name)  
{  
    this->age = age;  
    this->name = name;  
}  
  
Person::Person(const Person & obj)  
{  
    this->age = obj.age;  
    this->name = obj.name;  
}  
  
Person::Person(Person && obj)   
{  
    this->name = obj.name;  
    this->age = obj.age;  
}  
  
Person & Person::operator=(Person obj)  
{  
    this->age = obj.age;  
    this->name = obj.name;  
    return *this;  
}  
  
ostream & operator<<(ostream & cout, Person & obj)  
{  
    cout << obj.name << "的年龄为" << obj.age;  
    return cout;  
}  

在这里我们一定要注意:拷贝构造函数的形式:一定要将拷贝构造成员函数定义为const属性的

Main.cpp

#include "Stack.hpp"  
#include "Person.h"  
#include <iostream>  
#include <string>  
using namespace std;  
  
int main()  
{  
    Person Person_Obj1(19, "张三");  
    Stack<Person> Stack_Obj1;  
    Stack_Obj1.Push(Person_Obj1);  
    Stack_Obj1.ShowInf();  
}  

Stack.hpp

#include <iostream>  
using namespace std;  
#include <vector>  
#include <exception>  
#include <algorithm>  
  
template <class T>  
class Stack  
{  
private:  
    vector<T> element;  
public:  
    Stack() = default;  
    Stack(T obj);  
    Stack(vector<T> element);  
    Stack(Stack& obj);  
  
    void Push(T obj);  
    void Pop();  
    T Top();  
    void ShowInf();  
  
    ~Stack() = default;  
};  
  
template<class T>  
inline Stack<T>::Stack(T obj)  
{  
    this->element.push_back(obj);  
}  
  
template<class T>  
inline Stack<T>::Stack(vector<T> element)  
{  
    this->element = element;  
}  
  
template<class T>  
inline Stack<T>::Stack(Stack& obj)  
{  
    this->element = obj.element;  
}  
  
template<class T>  
inline void Stack<T>::Push(T obj)  
{  
    this->element.push_back(obj);  
}  
  
template<class T>  
inline void Stack<T>::Pop()  
{  
    if (this->element.empty())  
    {  
        throw out_of_range("empty stack");  
    }  
    this->element.pop_back();  
}  
  
template<class T>  
inline T Stack<T>::Top()  
{  
    if (this->element.empty())  
    {  
        throw out_of_range("empty stack");  
    }  
    return *(this->element.begin());  
}  
  
template<class T>  
inline void Stack<T>::ShowInf()  
{  
    if (this->element.empty())  
    {  
        throw out_of_range("empty stack");  
    }  
    for_each(this->element.begin(), this->element.end(), [](T& obj) {cout << obj << " "; });  
    cout << endl;  
}  

我们看到我们可以使用vector作为Stack栈的底层容器进行各项操作,这是依据Stack属性而言的,比如:我想频繁的入栈和出栈我们可以选择vector容器,Stack出栈入栈本质上来说就是在vector数组尾部删除插入元素。

但是queue队列就不一样了,queue是先入先出即从数组第一个元素前面插入元素,在最后一个末尾元素删除元素,这样的操作显然deque容器最为适合。

我们根据选择不同的底层容器来进行程序性能的提高,这就是容器适配器的作用。

猜你喜欢

转载自blog.csdn.net/weixin_45590473/article/details/112212090