C + + STL Learning queue

Source: Internet
Author: User

This article modifies the self http://www.cnblogs.com/hdk1993/p/5809180.html

1. Use queue to declare header file # include <queue>

2, the queue template class requires two template parameters, one is the element type, a container type, the element type is necessary, the container type is optional, the default is the Deque type.

3, the basic operation of the queue is:
Queue, as in example: Que.push (x); Connect X to the end of the queue.
Out of the team, as in Example: Que.pop (); The first element of the popup queue, note that the value of the popup element is not returned.
Access the first element of the team, such as: Que.front (), which is the first element to be pressed into the queue.
Access the tail element, such as: Que.back (), which is the element that was last pressed into the queue.
Determine if the queue is empty, as in example: Que.empty (), returns True when the queue is empty.
Access to the number of elements in the queue, as in example: Que.size ()

Code chestnut:

#include <iostream>#include<queue>using namespacestd;intMain () {queue<int>que;  for(intI=0;i<Ten; i++) {Que.push (i); }    if(!Que.empty ()) {cout<<"the queue is not empty. "<<Endl; } cout<<"the elements at the end of the team are:"; cout<<que.back () <<Endl; cout<<"To traverse a queue:"<<Endl;  while(Que.size ()) {cout<<que.front () <<Endl;    Que.pop (); }    if(Que.empty ()) {cout<<"The elements are all ejected and the queue is empty. "<<Endl; }    return 0;}

Output:

The queue is not empty.
The elements at the end of the team are: 9
To traverse a queue:
0
1
2
3
4
5
6
7
8
9
The elements are all ejected and the queue is empty.

C + + STL Learning queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.