[STL 12] Adapter - container adapter

1. Adapter

1. Introduction to Adapter

  • An adapter is essentially a mechanism for making one transaction behave like another type of transaction.
  • The adapter does not provide a real data structure implementation related to the stored form of the elements, and it does not support iterators

2. Advantages of using adapters

  • Let the programmer choose an appropriate underlying data structure.

3. Adapter type

  • container adapter
  • iterator adapter
  • function adapter

3.1, container adapter

  • A container adapter is a class template that encapsulates a sequence container, and it provides some different functions on the basis of a general sequence container.

3.2. Iterator Adapter

  • Iterator Adapter: is an STL component that modifies the interface of an iterator defined for some basic container.
  • Reverse iterators and insertion iterators belong to iterator adapters, which extend the functionality of iterators.

3.3. Function adapters

  • Function Adapter: Extends the functionality of other function objects by converting or modifying them. Predefined functors and other values ​​can be combined.
  • Function adapter type
    • Negator
    • binder
    • function pointer adapter

2. Container Adapter

1 Introduction

  • A simple understanding of the container adapter is to make the inapplicable serial containers (including vector, deque and list) applicable.

2. Classification

  • STL provides three kinds of container adapters, namely stack adapter, queue queue adapter and priority_queue priority queue adapter.
container adapter head File characteristic Base container filter The base container used by default
stack <stack > Insert and delete elements at the same end,
first-in, last-out (FILO) characteristics
The basic container must contain the following member functions:
empty()
size()
back()
push_back()
pop_back()
Basic containers that meet the conditions include vector, deque, and list.
therefore
queue <queue > Insert elements at one end, remove elements at the other end,
first-in, first-out (FIFO), insertion and deletion are faster
The basic container must contain the following member functions:
empty()
size()
front()
back()
push_back()
pop_front()
Basic containers that meet the conditions include deque and list.
therefore
priority_queue <priority_queue> It is also a queue, each element is given a priority to
control the order in which elements can be accessed
The basic container must contain the following member functions:
empty()
size()
front()
push_back()
pop_back()
Basic containers that meet the conditions include vector and deque.
vector

Three, stack adapter

1 Introduction

  • stack The stack adapter is a container with a single-ended opening (as shown in Figure 1). In fact, the container simulates the stack storage structure, that is, whether it is storing data in or fetching data from it, it can only be realized through this opening. operate.
  • First-in, last-out (first-in, last-out, FILO)

The beginning end of the stack adapter is often referred to as the top of the stack. Since the storage and retrieval of data can only be operated from the top of the stack, for accessing data, the stack adapter has such a feature that it can only access the topmost element in the adapter each time, and only remove the element at the top of the stack After that, the elements located on the stack can be accessed.

insert image description here

2. Constructor

template<
    class T,
    class Container = std::deque<T>
> class stack

T - the stored element type.
Container - The underlying container type used to store elements.

3. Member functions

  • 3.1. Element access
member function Function
top Access the top element of the stack
  • 3.2. Capacity
member function Function
empty Check if the underlying container is empty
size Returns the number of elements held
  • 3.3. Modify the operation member function
member function Function
push Insert an element to the top of the stack
location(C++11) Construct elements in-place on top
pop remove the top element of the stack
swap(C++11) exchange content

4、demo

#include <iostream>
#include <stack>
#include <list>
using namespace std;
int main()
{
    
    
    //构建 stack 容器适配器
    list<int> values{
    
     1, 2, 3 };
    stack<int, list<int>> my_stack(values);
    //查看 my_stack 存储元素的个数
    cout << "size of my_stack: " << my_stack.size() << endl;
    //将 my_stack 中存储的元素依次弹栈,直到其为空
    while (!my_stack.empty())
    {
    
    
        cout << my_stack.top() << endl;
        //将栈顶元素弹栈
        my_stack.pop();
    }
    return 0;
}

output

size of my_stack: 3
3
2
1

Three, queue adapter

1 Introduction

  • The queue container adapter has 2 openings, one of which is dedicated to input data and the other is dedicated to output data
  • First in, first out (first-in, first-out, FIFO)

The STL queue container adapter simulates the storage structure of the queue, so for any sequence that needs to be processed by the queue, using the queue container adapter is a good choice

insert image description here

2. Constructor

template<
    class T,
    class Container = std::deque<T>
> class queue

T - the stored element type.
Container - the underlying container used to store elements

3. Member functions

  • 3.1. Element access
member function Function
front access the first element
back access the last element
  • 3.2. Capacity
member function Function
empty Check if the underlying container is empty
size Returns the number of elements held
  • 3.3. Modify the operation member function
member function Function
push Insert an element at the end of the queue
location(C++11) Constructs elements in-place at the tail
pop remove the first element
swap exchange content

4、demo

#include <iostream>
#include <queue>
#include <list>
using namespace std;
int main()
{
    
    
    //构建 queue 容器适配器
    std::deque<int> values{
    
     1,2,3 };
    // 使用dqueue对 queue 进行初始化
    std::queue<int> my_queue(values);//{1,2,3}
    //查看 my_queue 存储元素的个数
    cout << "size of my_queue: " << my_queue.size() << endl;
    //访问 my_queue 中的元素
    while (!my_queue.empty())
    {
    
    
        cout << my_queue.front() << endl;
        //访问过的元素出队列
        my_queue.pop();
    }
    return 0;
}

output

size of my_queue: 3
1
2
3

Four, priority_queue adapter

1 Introduction

  • The storage and retrieval of elements in the priority_queue container adapter does not follow the "First in, First out" principle, but the "First in, Largest out" principle. The straightforward translation means that the elements of the advanced queue do not necessarily go out of the queue first, but the elements with the highest priority go out of the queue first.

  • So, how is the priority of the elements stored in the priority_queue container adapter evaluated? Quite simply, each priority_queue container adapter is created with a sorting rule. According to this rule, elements stored in this container adapter have priority.

举个例子,假设当前有一个 priority_queue 容器适配器,其制定的排序规则是按照元素值从大到小进行排序。根据此规则,自然是 priority_queue 中值最大的元素的优先级最高。
priority_queue 容器适配器为了保证每次从队头移除的都是当前优先级最高的元素,每当有新元素进入,它都会根据既定的排序规则找到优先级最高的元素,并将其移动到队列的队头;同样,当 priority_queue 从队头移除出一个元素之后,它也会再找到当前优先级最高的元素,并将其移动到队头。

基于 priority_queue 的这种特性,因此该容器适配器有被称为优先级队列.

2、构造函数

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;
  • typename T:指定存储元素的具体类型;

  • typename Container:指定 priority_queue 底层使用的基础容器,默认使用 vector 容器。

  • typename Compare:指定容器中评定元素优先级所遵循的排序规则,默认使用std::less按照元素值从大到小进行排序,还可以使用std::greater按照元素值从小到大排序,但更多情况下是使用自定义的排序规则。

其中,std::less 和 std::greater 都是以函数对象的方式定义在 头文件中。关于如何自定义排序规则,后续章节会做详细介绍。

3、成员函数

  • 3.1、元素访问
成员函数 功能
top 访问栈顶元素
  • 3.2、容量
成员函数 功能
empty 检查底层容器是否为空
size 返回容纳的元素数
  • 3.3、修改操作成员函数
成员函数 功能
push 向队列尾部插入元素
emplace(C++11) 在尾部原位构造元素
pop 删除首个元素
swap 交换内容

4、demo

#include <iostream>
#include <queue>
#include <array>
#include <functional>
using namespace std;
int main()
{
    
    
    //创建一个空的priority_queue容器适配器
    std::priority_queue<int>values;
    //使用 push() 成员函数向适配器中添加元素
    values.push(3);//{3}
    values.push(1);//{3,1}
    values.push(4);//{4,1,3}
    values.push(2);//{4,2,3,1}
    //遍历整个容器适配器
    while (!values.empty())
    {
    
    
        //输出第一个元素并移除。
        std::cout << values.top()<<" ";
        values.pop();//移除队头元素的同时,将剩余元素中优先级最大的移至队头
    }
    return 0;
}

demo

4 3 2 1

References:
1. C++ STL Container Library Chinese Documentation
2. STL Tutorial: C++ STL Quick Start
3. https://www.apiref.com/cpp-zh/cpp/header.html
4. https://en.cppreference. com/w/cpp/container
5. Bilibili - Xiajie Chat Algorithm - 8.2.1 Adapter and Iterator

Guess you like

Origin blog.csdn.net/junxuezheng/article/details/129781250