STL容器适配器:queue核心总结
std::stack是一种适配器容器,它提供了一个先进先出(FIFO)的数据结构。
std::queue的模板格式:
template< T, Container = std::deque<T> > class queue;
其中:
- T: 数据类型
- Container: 底层容器类型,默认是std::deque,还可以是std::list。
构造函数
queue<int> q1;                       // 无元素
queue<int> q2(q1);                   // 使用另外一个queue拷贝构造
queue<int> q3(std::move(q2));        // 使用另外一个queue移动构造
std::deque<int> dq {1, 2, 3, 4, 5};
queue<int> q4(dq);                   // 使用容器初始化
queue<int, std::list<int>> q5;       // 使用自定义底层容器
成员函数
访问
| 成员函数 | 函数说明 | 
|---|---|
| front() | 返回对队列中第一个元素的引用。 | 
| back() | 返回对队列中最后一个元素的引用。 | 
修改
| 成员函数 | 说明 | 
|---|---|
| push(const T& value) | 将一个新元素 value添加到队列的末尾。 | 
| emplace(Args&&... args) | 通过就地构造一个新元素,并将其添加到队列的末尾。 | 
| pop() | 移除队列的第一个元素。 | 
| swap(queue& other) | 与另一个 queue交换内容。 | 
容量
| 成员函数 | 说明 | 
|---|---|
| empty() | 如果队列中没有元素,则返回 true。 | 
| size() | 返回队列中元素的数量。 |