Use of # include <stack> and # include <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 is the element type and the other is the container type, but only the element type is necessary.
When the container type is not specified, the default container type is deque.
The sample code for defining a stack object is as follows:
Stack <int> S1;
Stack <string> S2;
Basic stack operations include:
Stack entry, for example, S. Push (X );
Out stack, for example, S. Pop (); note that the out stack operation only deletes the top element of the stack and does not return this element.
Top access stack, for example, S. Top ()
Judge whether the stack is empty. For example, S. Empty (). If the stack is empty, true is returned.
Number of elements in the access stack, for example, 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 also requires two template parameters, one of which is the element type and the other is the container class.
Type, element type is necessary, container type is optional, the default is deque type.
The sample code for defining a queue object is as follows:
Queue <int> q1;
Queue <double> Q2;

Basic queue operations include:
Join the queue, for example, Q. Push (x); Connect X to the end of the queue.
For example, Q. Pop (); the first element of the queue is displayed. Note that the value of the element to be popped up is not returned.
The first element of the access queue, such as Q. Front (), which is the first element to be pushed into the queue.
Access the elements at the end of the team, for example, Q. Back (), that is, the elements that are finally pushed into the queue.
Determines whether the queue is empty. For example, Q. Empty (). If the queue is empty, true is returned.
Number of elements in the access queue, for example, Q. Size ()

Use of # include <stack> and # include <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.