[STL] stack Stack

Source: Internet
Author: User

Stack

Header file and definition

 

# Include <stack>

Stack <long> mystack; // The following uses mystack as an example.

 

Usage

1. Add Element A to the stack: mystack. Push ();

2. Pull the top element of the stack to stack/to stack: mystack. Pop ();

3. Determine whether the stack is empty: mystack. Empty ()

4. Stack length: cout <stack. Size ();

5. Access the top element of the stack: cout <stack. Top ();

 

Notes

1. Check whether the stack is empty (or whether the stack length is 0) before exiting the stack or accessing the top element of the stack. Otherwise, an error occurs.

2. In the definition, do not always use a regular Int .. Long long

3. (mystack. Empty () is a judgment condition. You must habitually add a layer () to the periphery to avoid further modification.

 

Exercise

Http://wikioi.com/problem/3137/

Http://wikioi.com/problem/3138/

Http://wikioi.com/problem/3139/

Three templates

Corresponding program

// Exercise 1 # include <iostream> # include <stack> using namespace STD; int N, I, type, A; int main () {stack <int> st; cin> N; for (I = 1; I <= N; I ++) {CIN> type; If (type = 1) {CIN>; st. push (a);} If (type = 2) ST. pop ();} If (St. empty () {cout <"impossible! "<Endl ;}else {cout <st. Top () <Endl ;}}
Exercise 1
// Exercise 2 # include <iostream> # include <stack> using namespace STD; int N, I, type, A; int main () {stack <long> st; cin> N; for (I = 1; I <= N; I ++) {CIN> type; If (type = 1) {CIN>; st. push (a) ;}if (type = 2) {If (! St. Empty () {st. Pop () ;}else {cout <"impossible! "<Endl; return 0 ;}}if (St. Empty () {cout <" impossible! "<Endl ;}else {cout <st. Top () <Endl;} return 0 ;}
Exercise 2
// Exercise 3 # include <iostream> # include <stack> using namespace STD; int N, I, type, A; int main () {stack <long> st; cin> N; for (I = 1; I <= N; I ++) {CIN> type; If (type = 1) {CIN>; st. push (a) ;}if (type = 2) {If (! St. empty () {St. pop () ;}}if (type = 3) {cout <St. top () <Endl ;}} return 0 ;}
Exercise 3

To write STL .. So don't worry about code speed, length, or anything .. Learning STL is the right path...

 

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.