stack 介紹
棧是一種容器適配器,特別為后入先出而設計的一種(LIFO ),那種數據被插入,然后再容器末端取出棧實現了容器適配器,這是用了一個封裝了的類作為他的特定容器,提供了一組成員函數去訪問他的元素,元素從特定的容器,也就是堆棧的頭取出袁術。
這個基礎的容器可能是任何標準的容器類,和一些其他特殊設計的模板類,唯一的要求就是要支持一下的操作
[cpp] view plain copy<span style="font-size:16px;"><strong>?</strong>back() ?push_back() ?pop_back()</span>
因此,標準的容器類模板vector, deque 和list可以使用,默認情況下,如果沒有容器類被指定成為一個提別的stack 類,標準的容器類模板就是deque 隊列。
//常用模型stackr<int,vector<int>>;stack<int,deque<int>>;stack<int,list<int>>;//默認stack<int,deque<int>>;實現C++ STL,棧有兩個參數。
|
參數示意:
T: 元素類型Container: 被用于存儲和訪問元素的的類型成員函數
stack::stack
explicit stack ( const Container& ctnr = Container() );
用于構造一個棧適配器對象。
ctnrContainer objectContainer is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).[cpp] view plain copy// test_stack.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include <stack> #include <vector> #include <deque> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { deque<int> mydeque(2,100); vector<int> myvector(2,200); stack<int> first; stack<int> second(mydeque); stack<int,vector<int> > third; stack<int,vector<int> > fourth(myvector); cout << "size of first: " << (int) first.size() << endl; cout << "size of second: " << (int) second.size() << endl; cout << "size of third: " << (int) third.size() << endl; cout << "size of fourth: " << (int) fourth.size() << endl; return 0; }output:
size of first: 0size of second: 3size of third: 0size of fourth: 2 |
stack::empty
bool empty ( ) const;判斷是否為空。
Return Value
true if the container size is 0, false otherwise.
[cpp] view plain copy// stack::empty #include <iostream> #include <stack> using namespace std; int main () { stack<int> mystack; int sum (0); for (int i=1;i<=10;i++) mystack.push(i); while (!mystack.empty()) { sum += mystack.top(); mystack.pop(); } cout << "total: " << sum << endl; return 0; }Output:
total: 55 |
stack::pop
void pop ( );
在棧的頂部移除元素。
[cpp] view plain copy // stack::push/pop #include <iostream> #include <stack> using namespace std; int main () { stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); cout << "Popping out elements..."; while (!mystack.empty()) { cout << " " << mystack.top(); mystack.pop(); } cout << endl; return 0; }
Output:
Popping out elements... 4 3 2 1 0 |
void push ( const T& x );在棧頂添加元素
[cpp] view plain copy // stack::push/pop #include <iostream> #include <stack> using namespace std; int main () { stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); cout << "Popping out elements..."; while (!mystack.empty()) { cout << " " << mystack.top(); mystack.pop(); } cout << endl; return 0; }
Output:
Popping out elements... 4 3 2 1 0 |
size_type size ( ) const;計算棧對象元素個數
|
Output:
0. size: 01. size: 52. size: 4 |
value_type& top ( );const value_type& top ( ) const;返回棧頂元素
[cpp] view plain copy// test_stack.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include <stack> #include <vector> #include <deque> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { stack<int> mystack; mystack.push(10); mystack.push(20); mystack.top()-=5; cout << "mystack.top() is now " << mystack.top() << endl; return 0; }Output:
mystack.top() is now 15 |
新聞熱點
疑難解答
圖片精選