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...