c/c++语言开发共享数据结构:队列queue 函数push() pop size empty front back

队列queue: push() pop() size() empty() front() back() …


队列queue

  push()  pop()  size()  empty()  front()  back()

  1. push()  队列中由于是先进先出,push即在队尾插入一个元素,如:可以输出:hello world!
queue<string> q;  q.push("hello world!");  q.push("china");  cout<<q.front()<<endl;
  1. pop() 将队列中最靠前位置的元素拿掉,是没有返回值的void函数。如:可以输出:china,原因是hello world!已经被除掉了。
queue<string> q;  q.push("hello world!");  q.push("china");  q.pop();  cout<<q.front()<<endl;
  1. size() 返回队列中元素的个数,返回值类型为unsigned int。如:输出两行,分别为0和2,即队列中元素的个数。
queue<string> q;  cout<<q.size()<<endl;  q.push("hello world!");  q.push("china");  cout<<q.size()<<endl;
  1. empty() 判断队列是否为空的,如果为空则返回true。如:输出为两行,分别是1和0。因为一开始队列是空的,后来插入了两个元素。
queue<string> q;  cout<<q.empty()<<endl;  q.push("hello world!");  q.push("china");  cout<<q.empty()<<endl;
  1. front() 返回值为队列中的第一个元素,也就是最早、最先进入队列的元素。如:输出值为两行,分别是hello world!和china。只有在使用了pop以后,队列中的最早进入元素才会被剔除。
queue<string> q;  q.push("hello world!");  q.push("china");  cout<<q.front()<<endl;  q.pop();  cout<<q.front()<<endl
  1. back() 返回队列中最后一个元素,也就是最晚进去的元素。如:输出值为china,因为它是最后进去的。这里back仅仅是返回最后一个元素,并没有将该元素从队列剔除掉。
queue<string> q;  q.push("hello world!");  q.push("china");  cout<<q.back()<<endl;

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/602063.html

(0)
上一篇 2021年5月11日
下一篇 2021年5月11日

精彩推荐