q.front():引用队列第一个元素
q.back():引用队列最后一个元素
q.push():入队
q.pop():出队
q.empty():队列判空,空返回1,不为空返回0
q.emplace():指定位置插入元素,但由于这里是队列,所以效果与push一样
q.size():获取队列容量
q.swap():交换两个队列中同类型的元素
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- queue
q; - queue
p; - p.push("good");
- cout<
empty()< - //入队
- q.push("hello");
- q.push("queue");
- q.push("nine");
- q.push("twelve");
- //出队
- q.pop(); //队列q中现有内容:queue nine twelve
- //引用队列第一个元素
- cout<
front()< - //引用队列最后一个元素
- cout<
back()< - //判断队列是否为空,为空返回1,不为空返回0
- cout<
empty()< - //由于队列是FIFO,不能随意插入,所以这里第一个参数默认为队尾
- q.emplace("last");
- //获得队列大小
- cout<
size()< - //交换两个队列中的同类型的内容
- q.swap(p);
- cout<<"交换后队列p中内容:"<
- //由于for循环里面在出队,导致队列p的大小在减小,所以for循环里面不能用p.size作为判断条件
- int j=p.size();
- for(int i=0;i
- cout<
front()< - p.pop();
- }
- cout<<"交换后队列q中内容:"<
- cout<
front()< - cout<
back()< - return 0;
- }
- 1
- queue
- twelve
- 0
- 4
- 交换后队列p中内容:
- queue
- nine
- twelve
- last
- 交换后队列q中内容:
- good
- good
s.push():压栈
s.pop():弹栈
s.empty():栈判空,空返回1,不为空返回0
s.emplace():指定位置插入元素,但由于这里是栈,所以效果与push一样
s.size():获取栈容量
s.swap():交换两个栈中同类型的元素
s.top():获取栈顶元素
- #include
- #include
- using namespace std;
- int main()
- {
- stack
s; - //压栈
- s.push("hello");
- s.push("stack");
- s.push("nine");
- s.push("twelve");
- //弹栈
- s.pop();
- //判空
- s.empty();
- //获取栈的大小
- cout<
size()< - //指定位置插入元素,由于是栈,所以默认在栈顶插入
- s.emplace("emplace");
- //获取栈顶元素
- cout<
top()< - stack
s1; - s.swap(s1);
- int flag_s = s.size();
- cout<<"栈s中的内容:"<
- for(int i=0;i
- cout<
top()< - s.pop();
- }
- int flag_s1 = s1.size();
- cout<<"栈s中的内容:"<
- for(int i=0;i
- cout<
top()< - s1.pop();
- }
- return 0;
- }
- 3
- emplace
- 栈s中的内容:
- 栈s中的内容:
- emplace
- nine
- stack
- hello
-
相关阅读:
后端面经学习自测(二)
python 中文字符转换unicode及Unicode 编码转换为中文
jQuery_显式迭代each()
MySQL、SQL Server、Hive对时间格式化
每天一道C语言编程:排队买票
关于Mybaits缓存....
Qt day4
数据链路层——MAC帧、ARP协议详解
基于ESP8266和微型舵机的远程灯控制系统
soh估计:Data-driven prediction of battery cycle life before capacity degradation
-
原文地址:https://blog.csdn.net/hold_the_key/article/details/126814257