How to use C + + Stl--stack/queue

Source: Internet
Author: User

1. Stack
The stack template class is defined in the <stack> header file.
The stack template class requires two template parameters, one element type, one container type, but only the element type is necessary
, the default container type is deque when you do not specify a container type.
The sample code that defines the stack object is as follows:
stack<int> S1;
Stack<string> S2;
The basic operations of stack are:
Into the stack, as in example: S.push (x);
Out of the stack, as in example: S.pop (); Note that the stack operation simply deletes the top element of the stack and does not return the element.
Access to the top of the stack, as in example: S.top ()
Determine the stack null, as an example: S.empty (), returns True when the stack is empty.
Access the number of elements in the stack, such as: S.size ().

2. Queue
The queue template class is defined in the <queue> header file.
Similar to the Stack template class, the queue template class requires two template parameters, one for the element type and one for the container class
Type is required, the type of the container is optional, and the default is the Deque type.
The sample code that defines the queue object is as follows:
Queue<int> Q1;
Queue<double> Q2;

The basic operations of the queue are:
Queue, as in example: Q.push (x); Connect X to the end of the queue.
Out of the team, as in Example: Q.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: Q.front (), which is the first element to be pressed into the queue.
Access the tail element, such as: Q.back (), which is the element that was last pressed into the queue.
Determine if the queue is empty, as in example: Q.empty (), returns True when the queue is empty.
Access to the number of elements in the queue, as in example: Q.size ()

How to use C + + Stl--stack/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.