"C + +" STL: Stack

Source: Internet
Author: User

C++stack (Stack) is an adaptation of a container that implements an advanced post-out data structure (FILO)

You need to include the #include<stack> header file when using this container;

The sample code that defines the stack object is as follows:

stack<int>s1;

stack<string>s2;

The basic operations of stack are:

1. Into the stack: such as S.push (x);

2. Out of stack: such as S.pop (). Note: The stack operation simply removes the element at the top of the stack and does not return the element.

3. Access the top of the stack: such as s.top ();

4. Determine the empty stack: such as S.empty (). Returns True when the stack is empty.

5. Access the number of elements in the stack, such as s.size ();

Here's a simple example:

[CPP]View Plaincopyprint?
  1. #include <iostream>
  2. #include <stack>
  3. Using namespace std;
  4. int main (void)
  5. {
  6. stack<double>s; Define a stack
  7. For (int i=0;i<10;i++)
  8. S.push (i);
  9. While (!s.empty ())
  10. {
  11. printf ("%lf\n", S.top ());
  12. S.pop ();
  13. }
  14. cout<<"The number of elements in the stack is:" <<s.size () <<endl;
  15. return 0;
  16. }

"C + +" STL: Stack

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.