Stack (stack) is a LIFO data structure, that is, LIFO (last in first out), and finally the elements to be added to the stack will be first taken out, on the same side of the stack to insert and remove data, this section is called "Stack top."
Using the STL stack requires an include header file <stack>
Construction
Template <class T, class Container = deque<t> > class stack;
As above, there are two parameters in the angle brackets, the first is T, which indicates the type of data stored in the stack, such as int,double, or struct.
The second parameter indicates the container type of the underlying implementation, which indicates the internal implementation of the stack, such as vector,deque,list. If you do not specify it, Deque (double-ended queue) is used by default. Of course, you do not need to specify this parameter in general.
Look directly at chestnuts,
//Construction Stacks#include <iostream>#include<stack>//Use stack stack#include <vector>//Vector#include <deque>//dequeusing namespacestd;intMain () {stack<int> First;//constructs an empty stack that holds the type int (the default underlying container is Deque), size=0. Is this the simplest and most common way? (?????)? If you feel that other way is too complicated, you can use this one.deque<int> Mydeque (3, -);//constructs a double-ended queue that contains 3 int elementsstack<int> second (mydeque);//construct a stack with your own double-ended queue (size=3)Stack<int,vector<int> > Third;//indicates that a stack is implemented with vector (storing int), empty stack size=0Vector<int> Myvector (2, $);//constructs a vector that holds 2 elementsstack<int,vector<int> > Fourth (myvector);//construct a stack with your own vector, size=2//size of output four stackscout <<"size of first:"<< first.size () << Endl; cout<<"size of second:"<< second.size () << Endl; cout<<"size of third:"<< third.size () << Endl; cout<<"size of Fourth:"<< fourth.size () << Endl; return 0;}
Output Result:
0302
member functions
First, some of the commonly used, directly to see chestnuts? (?•?? •??)?
#include <iostream>#include<stack>using namespacestd;intMain () {stack<int>Mystack; for(intI=0; i<5; ++i) Mystack.push (i);//The push function adds a parameter element to the stack, with no return value (for example, looping over the 0,1,2,3,4 into the stack, note that the top element of the stack is 4)cout <<"Size:"<< mystack.size () << Endl;//the SIZE function returns the stack (5 elements in this case, size=5) while(!mystack.empty ())//The empty function returns a bool value and returns True if the stack is empty, otherwise false{cout<<' '<< Mystack.top ();//The return value of the top function is the top element of the stack (note that the top element is not deleted)Mystack.pop ();//the pop function deletes the top element of the stack without a return value} cout<<Endl; return 0;}
Operation Result:
5 4 3 2 1 0
See another set of chestnuts:
#include <iostream>#include<stack>using namespacestd;structNode {intb; Node (intXinty) {a= x; b =y; }};intMain () {stack<Node>Mystack; Mystack.emplace (1,2);//The emplace function can add an element to the stack, unlike the push: Emplace can pass directly to the parameters of node's constructor and add the constructed element to the stack//(Mystack.push); //compilation does not pass, to achieve the above effect requires manual construction, such as Mystack.push (Node);Node p =Mystack.top (); cout<< P.a <<" "<< p.b <<Endl; Stack<Node>My2; My2.swap (Mystack); //swap function can exchange elements of two stackscout << mystack.size () <<" "<< my2.size () <<Endl; return 0;}
Operation Result:
1 2 0 1
The above is the general operation of the stack
C + + STL stack