【Containers】queue

简述

队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。

队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。

特点

  1. 先入先出,后入后出。
  2. 除头尾节点之外,每个元素有一个前驱,一个后继。

基本用法

头文件:#include<queue>

构造方法:

template < class T, class Container = deque<T> > class queue;
// 参数T为元素类型,Container为用于存储和访问元素的容器类型,默认为deque
  1. queue<T> q1;                                        q1是一个空的queue,元素类型为T,执行默认初始化
  2. queue<T> q2(q1);                                  q2包含q1中所有元素的副本
  3. queue<T> q2=q1;                                   等价于q2(q1),q2包含q1中所有元素的副本
  4. queue<T> q3(d1);                                  q3包含deque<T> d1中的所有元素的副本,注意类型要一致哦

基本函数操作:

函数功能
empty()判断队是否为空,为空返回ture
size()返回队内元素个数
front()返回队首元素
push(a)向队尾插入一个元素a
emplace(a)向队尾插入一个元素a
pop()删除一个队首元素
swap()交换两个队内所有元素

代码示例:

#include<iostream>
#include<queue>
#include<vector>
#include<deque>
using namespace std;

int main()
{
    int a[2]={1,2};
    deque<int> d1(2,3);
    queue<int> q1;
    for(int i=0;i<2;i++)q1.push(a[i]);
    queue<int> q2(q1);
    queue<int> q3(d1);
    while(!q1.empty()){
        cout <<q1.front()<<" ";
        q1.pop();
    }cout<<endl;
    while(!q2.empty()){
        cout <<q2.front()<<" ";
        q2.pop();
    }cout<<endl;
    while(!q3.empty()){
        cout <<q3.front()<<" ";
        q3.pop();
    }cout<<endl;
    return 0;
}

Output
 1  2
 1  2
 3  3

 

### C++ STL Queue Usage and Examples In C++, `std::queue` is part of the Standard Template Library (STL), which provides a convenient way to implement queues using templates. A queue follows the First-In-First-Out (FIFO) principle where elements are inserted from one end called rear and deleted from another end known as front. To use `std::queue`, include `<queue>` header file. Here’s how one can define and initialize a queue: ```cpp #include <iostream> #include <queue> int main() { std::queue<int> q; } ``` Adding elements into the queue uses member function `push()` or `emplace()`. Removing elements employs `pop()`. Accessing but not removing the first element utilizes `front()`, while checking whether the queue is empty relies on `empty()`. To get the number of elements currently stored within the container adaptor, apply `size()` method[^1]. Below demonstrates basic operations with integers in a queue: ```cpp #include <iostream> #include <queue> void showQueue(std::queue<int> gq){ while (!gq.empty()){ std::cout << '\t' << gq.front(); gq.pop(); } } int main(){ std::queue<int> gquiz; gquiz.push(10); gquiz.push(20); std::cout << "The queue gquiz is : "; showQueue(gquiz); std::cout<<"\ngquiz.size() : "<<gquiz.size(); return 0; } ``` For more complex data structures like strings or custom objects, similar methods apply without modification needed since these functions work generically over different types through template instantiation mechanisms provided by C++. However, when dealing specifically with boolean values inside vectors (`std::vector<bool>`), special attention should be paid due to its unique implementation involving proxies rather than direct storage of bools [^2]. This does not affect standard queues directly unless such specialized containers become involved indirectly via contained elements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值