C++ STL queue小结

一  queue需要的头文件

需要头文件如下:
#include<queue>



二  queue的声明

queue<数据结构> 队列名
例:

queue<int> q1;

queue<char> q2;

queue<node> q3; //node 为结构体名

queue<int> q[4];



三  queue的的基本操作

先定义一个整型队列 q
queue<int> q;
q.size();//返回q里元素个数
q.empty();//查看q是否为空,空返回true,否则返回false
q.push(w);//在q的队尾插入w
q.pop();//删掉q的第一个元素
q.front();//返回q的第一个元素

q.back();//返回q的末尾元素



四  实际操作

#include<stdio.h>
#include<queue>
using namespace std;

int main() {
    queue<int> q[4];//声明整型队列数组
    int head[4];
    int add1=10;
    char add2='w';

//四个队列分别加入整型变量,整型常量,字符变量,字符常量,都可行
    q[0].push(add1);
    q[1].push(5);
    q[2].push(add2);
    q[3].push('a');

    head[0]=q[0].front();
    head[1]=q[1].front();
    head[2]=q[2].front();
    head[3]=q[3].front();
    printf("%d %d %d %d\n\n",head[0],head[1],head[2],head[3]);

    q[0].push(head[1]);
    int len,tail,flag;
    len=q[0].size();
    tail=q[0].back();
    flag=q[0].empty();
    printf("len:%d\ntail:%d\nflag:%d\n\n",len,tail,flag);

    q[0].pop(); //队首出队
    head[0]=q[0].front();
    printf("head:%d\n\n",head[0]);
    return 0;
}
/*
输出结果
10 5 119 97

len:2
tail:5
flag:0

head:5
*/



五  关于结构体的操作


#include<stdio.h>
#include<queue>
using namespace std;

struct node
{
    int x,y;
}st,head,tail;

int main() {
    int len,i,flag;
    queue<node> q;

    for(i=0;i<3;i++){
        st.x=i,st.y=i+1;
        q.push(st);
    }

    q.pop();
    len=q.size();
    head=q.front();
    tail=q.back();
    flag=q.empty();

    printf("head.x\thead.y\ttail.x\ttail.y\tlen\tflag\n");
    printf("%d\t%d\t%d\t%d\t%d\t%d\n",head.x,head.y,tail.x,tail.y,len,flag);
    return 0;
}
/*
输出结果
head.x  head.y  tail.x  tail.y  len     flag
1       2       2       3       2       0
*/




六  关于back()与front()的问题

1.如果队列有元素入队后被清空,队首元素默认为0,队尾元素会是最后一个消失元素。(不同编译器可能不同)

#include<stdio.h>
#include<queue>
using namespace std;

int main() {
    queue<int> q;
    int head,tail,len;

    q.push(1);
    q.push(2);

    head=q.front();
    tail=q.back();
    len=q.size();
    printf("head\ttail\tlen:\n");
    printf("%d\t%d\t%d\n\n",head,tail,len);

    q.pop();
    head=q.front();
    tail=q.back();
    len=q.size();
    printf("%d\t%d\t%d\n\n",head,tail,len);

    q.pop();
    head=q.front();
    tail=q.back();
    len=q.size();
    printf("%d\t%d\t%d\n\n",head,tail,len);

    tail=10,head=10,len=10;
    head=q.front();
    tail=q.back();
    len=q.size();
    printf("%d\t%d\t%d\n\n",head,tail,len);
    return 0;
}
/*
输出结果
head    tail    len:
1       2       2

2       2       1

0       2       0

0       2       0
*/

2.如果队列还没有元素入队操作,使用back和front会有未知bug(应该也没人会在这种情况使用front和back←_←

#include<stdio.h>
#include<queue>
using namespace std;

int main() {
    queue<int> q;
    int head,tail,len;
    head=q.front();
//tail=q.back();无法运行
    len=q.size();
    printf("%d\t%d\n\n",head,len);

    head=0;
    head=q.front();
    printf("%d\t%d\n\n",head,len);
    return 0;
}
/*
输出结果
45416640        0

45416640        0
*/



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的代码展示了使用STL库中的queue容器的示例。queue是一种先进先出(FIFO)的数据结构,它允许在队尾插入元素,在队首删除元素。在这个示例中,首先创建了一个CData的对象test1,并将其加入到了dataQueue中,然后通过front()函数获取dataQueue的队首元素并赋值给data对象,最后通过pop()函数将队首元素从dataQueue中弹出。 引用说明了queue的底层实现类型,默认为std::deque,但也可以是其他类型,只要支持相应的操作,如front、back、push_back和pop_front等。 引用列举了queue常用的成员函数和功能。其中,empty()函数用于判断queue是否为空,size()函数用于返回queue中元素的个数,front()函数返回queue的第一个元素的引用,back()函数返回queue的最后一个元素的引用,push()函数用于在queue的尾部添加一个元素的副本,emplace()函数用于在queue的尾部直接添加一个元素,push(T&& obj)函数以移动的方式在queue的尾部添加元素,pop()函数用于删除queue中的第一个元素,swap()函数用于交换两个queue容器的元素。 综上所述,引用的代码示例展示了使用STL库中的queue容器的基本操作,包括插入、删除和访问元素的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [c++ stl queue的使用](https://blog.csdn.net/tianyexing2008/article/details/126356034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++STLqueue的简单使用](https://blog.csdn.net/lady_killer9/article/details/79261798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值