更多關於STL文章——STL學習筆記
容器適配器
stack
Class stack<> 實現出一個 stack(也稱爲LIFO,後進先出)。你可以使用 push() 將任意數量的元素放入 stack,也可以使用 pop() 將元素依其插入的相反次序從容器中移出(此即所謂“後進先出[LIFO]”)。
class stack 定義如下:
namespace std{
template <typename T, typename Container = deque<T>>
class stack;
}
第一個 template 參數代表元素類型。帶有默認值的第二個 template 參數用來定義 stack 內部存放元素的實際容器,默認爲 deque。之所以選擇 deque 而非 vector ,是因爲 deque 移除元素時會釋放內存,並且不必在重分配,並且不必在重分配時複製全部元素。
Stack 的實現中只是很單純地把各項操作轉化爲內部容器的對應調用。你可以使用任何sequence容器支持 stack,只要它們提供以下成員函數:back()、push_back() 和 pop_back()。
template<typename Tp, typename Sequence = deque<Tp> >
class stack{
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
typedef typename Sequence::size_type size_type;
typedef Sequence container_type;
}
類型名稱 | 定義 |
---|---|
value_type | 元素的類型 |
reference | 用以指向元素之reference類型 |
const_reference | 用以指向只讀元素之reference類型 |
size_type | 不帶正負號的整數類型,用來表現大小 |
container_type | 內部容器的類型 |
- 構造
explicit stack (const container_type& ctnr);
//構造一個stack,其內部容器被初始化爲ctnr的副本。
explicit stack (container_type&& ctnr = container_type());
//構造一個stack,其內部容器通過移動構造來獲取ctnr的值。
template <class Alloc> explicit stack (const Alloc& alloc);
//構造一個stack,其內部容器使用alloc作爲參數構造。
template <class Alloc> stack (const container_type& ctnr, const Alloc& alloc);
//構造一個stack,其內部容器使用cntr和alloc作爲參數構造。
template <class Alloc> stack (container_type&& ctnr, const Alloc& alloc);
//同上,移動構造
template <class Alloc> stack (const stack& x, const Alloc& alloc);
//構造一個stack,其內部容器使用x的內部容器作爲第一個參數,而alloc作爲第二個來構造。
template <class Alloc> stack (stack&& x, const Alloc& alloc);
//同上,移動構造
#include<iostream>
#include<stack>
#include<list>
#include<deque>
using namespace std;
int main()
{
deque<int> mydeck (3,100); //創建一個deque含3個100
list<int> mylist (2,200); //創建一個list含2個200
stack<int> first; //創建一個空的堆棧
stack<int> second (mydeck); //用mydeck的拷貝作爲堆棧的初始值
//第二個參數模板默認即爲 deque,因此可以不寫。
stack<int,list<int>> third; //空堆棧,列表作爲底層容器
stack<int,list<int>> fourth (mylist); //用mylist的拷貝作爲堆棧的初始值
cout << "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;
}
結果爲:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2
下面爲其成員函數:
- empty
判斷容器是否爲空
bool empty() const;
- size
返回當前的元素個數
size_type size() const;
- top
返回最後一個安插的元素
reference& top();
const_reference& top() const;
- push
安插一個元素
void push (const value_type& val);
void push (value_type&& val);
- emplace
構造與安插元素
template <class… Args> void emplace (Args&&… args);
- pop
移除容器內的下一個元素
void pop();
- swap
交換兩個容器的內容
void swap (stack& x);
圖片來源:c語言中文網
一個stack的使用例子
題目來源:洛谷P1739
題目描述
假設一個表達式有英文字母(小寫)、運算符(+,—,*,/)和左右小(圓)括號構成,以“@”作爲表達式的結束符。請編寫一個程序檢查表達式中的左右圓括號是否匹配,若匹配,則返回“YES”;否則返回“NO”。表達式長度小於255,左圓括號少於20個。
輸入格式
一行:表達式
輸出格式
一行:“YES” 或“NO”
輸入輸出樣例
輸入 #1
2*(x+y)/(1-x)@
輸出 #1
YES
輸入 #2
(25+x)(a(a+b+b)@
輸出 #2
NO
說明/提示
表達式長度小於255,左圓括號少於20個
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
string str;
getline(cin,str);
stack<char> buf;
for(auto& i:str){
if(i=='(') //如果等於 ( 進棧
buf.push(i);
else if(i==')'){ //如果等於 ) 進行判斷
if(!buf.empty()) //如果棧不爲空,將最後一個進棧的 ( 彈出,說明該括號已經匹配
buf.pop();
else{ //如果此時棧爲空,說明不匹配,直接輸出NO 退出
cout<<"NO"<<endl;
return 0;
}
}
}
if(buf.empty()) //如果最後棧爲空 說明全部匹配,輸出 YES
cout<<"YES"<<endl;
else //如果最後棧裏面還有 ( 說明未匹配,輸出 NO
cout<<"NO"<<endl;
return 0;
}