原創作品,轉載請注明出處:http://www.cnblogs.com/shrimp-can/p/5283207.html
棧是后入先出的。成員函數有:
1.棧的聲明
std::deque<int> mydeque (3,100); // deque with 3 elements
std::vector<int> myvector (2,200); // vector with 2 elements
std::stack<int> first; // empty stack
std::stack<int> second (mydeque); // stack initialized to copy of deque
std::stack<int,std::vector<int> > third; // empty stack using vector
std::stack<int,std::vector<int> > fourth (myvector);
std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n';
結果為:0 3 0 2
2.bool empty() const
判斷棧是否為空
stack<int> c; c.empty()
3.size_type size() const
返回棧中元素數量
c.size();
4.value_type& top();
const value_type &top() const;
返回棧頂元素
c.top();
5.void push(const value_type& val)
在棧頂插入一個元素
c.push(value);
6.void emplace(args&& args);
在棧頂增加一個元素
c.emplace(value)
7.void pop()
出棧,即刪除棧頂元素
c.pop();
8.void swap (stack& x);
交換兩個棧中的內容
c.swap(d);
9.與vector一樣,重載了運算符:== != < <= > >=
參考:http://www.cplusplus.com/reference/stack/stack/