範本容器配接器類別,其提供的功能限制將對項目的存取限制為最近加入一些基礎容器類型的項目。 當 stack 請務必清楚只有 stack 作業在容器上執行時,就會使用 類別。
語法
template <class Type, class Container= deque <Type>>
class stack
參數
Type
要存放在堆疊中的項目資料類型。
Container
用來實作堆疊的基礎容器類型。 預設值為類別 deque<Type>。
備註
stack 物件第一個樣板參數中規定之類別 Type 的元素與 同義 value_type ,而且必須符合第二個樣板參數所規定之基礎容器類別 Container 中的元素類型。 Type必須可指派,才能複製該型別的物件,並將值指派給該類型的變數。
適用於堆疊的合適基礎容器類別包括deque、list類別和vector類別,或支援、 push_back和 pop_back作業back的任何其他時序容器。 基礎容器類別會封裝在容器介面卡內,它只會公開有限的序列容器成員函式集做為公用的介面。
stack如果和類別的元素相等可比較,而且只有在 類別的元素小於可比較時,而且只有在 類別的TypeType元素小於可比較時,物件才會相等。
類別
stack支持最後一個先出 (LIFO) 資料結構。 就好像盤子的堆疊一樣,這是一種較為貼切好記的類比。 項目 (盤子) 可能會插入、檢查,或只從堆疊頂端移除,這是基底容器尾端的最後一個項目。 只存取 top 元素的限制是使用stack類別的原因。類別
queue支援先進先出 (FIFO) 數據結構。 就好像人們排隊等候銀行櫃員一樣,這是一種較為貼切好記的類比。 項目 (人) 可能會加入隊伍的尾端,以及從隊伍的前面移除。 隊伍的前端和後端都可能會進行檢查。 以這種方式只存取前端和後方元素的限制,是使用queue類別的 fur 的原因。類別
priority_queue會排序其元素,讓最大元素一律位於頂端位置。 它支援插入項目,以及檢查和移除頂端項目。 就好像依照年齡、身高或某些其他條件來排列一群人一樣,這是一種較為貼切好記的類比。
成員
建構函式
| 名稱 | 描述 |
|---|---|
stack |
建構空的,或是基底容器物件複本的 stack。 |
Typedefs
| 名稱 | 描述 |
|---|---|
container_type |
提供基底容器以讓 stack 調整的類型。 |
size_type |
不帶正負號的整數類型,可以表示 stack 中的項目數。 |
value_type |
此類型代表儲存為 stack 項目的物件類型。 |
函式
| 名稱 | 描述 |
|---|---|
empty |
測試 stack 是否為空白。 |
pop |
從 stack 頂端移除項目。 |
push |
將項目加入 stack 的頂端。 |
size |
傳回 stack 中項目的數目。 |
top |
傳回 stack 頂端項目的參考。 |
container_type
提供要配接之基底容器的類型。
typedef Container container_type;
備註
此類型是範本參數 Container的同義字。 這三個C++標準連結庫序列容器類別— vector 類別、 list 類別和默認類別 deque —都符合做為物件基底容器 stack 的需求。 您也可以使用滿足該要求的使用者定義類型。
如需 的詳細資訊Container,請參閱類別主題的stack一節。
範例
如需如何宣告和使用 container_type的範例stack::stack,請參閱範例。
empty
測試堆疊是否為空。
bool empty() const;
傳回值
true 如果堆疊是空的,則為 ; false 如果堆疊是空的,則為 。
範例
// stack_empty.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>
int main( )
{
using namespace std;
// Declares stacks with default deque base container
stack <int> s1, s2;
s1.push( 1 );
if ( s1.empty( ) )
cout << "The stack s1 is empty." << endl;
else
cout << "The stack s1 is not empty." << endl;
if ( s2.empty( ) )
cout << "The stack s2 is empty." << endl;
else
cout << "The stack s2 is not empty." << endl;
}
The stack s1 is not empty.
The stack s2 is empty.
pop
從堆疊頂端移除項目。
void pop();
備註
堆疊不得為空,才能套用成員函式。 堆疊頂端是最近新增項目所佔用的位置,也是位於容器結尾的最後一個項目。
範例
// stack_pop.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>
int main( )
{
using namespace std;
stack <int> s1, s2;
s1.push( 10 );
s1.push( 20 );
s1.push( 30 );
stack <int>::size_type i;
i = s1.size( );
cout << "The stack length is " << i << "." << endl;
i = s1.top( );
cout << "The element at the top of the stack is "
<< i << "." << endl;
s1.pop( );
i = s1.size( );
cout << "After a pop, the stack length is "
<< i << "." << endl;
i = s1.top( );
cout << "After a pop, the element at the top of the stack is "
<< i << "." << endl;
}
The stack length is 3.
The element at the top of the stack is 30.
After a pop, the stack length is 2.
After a pop, the element at the top of the stack is 20.
push
將專案加入至堆疊頂端。
void push(const Type& val);
參數
val
加入至堆疊頂端的項目。
備註
堆疊頂端是最近新增項目所佔用的位置,也是位於容器結尾的最後一個項目。
範例
// stack_push.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>
int main( )
{
using namespace std;
stack <int> s1;
s1.push( 10 );
s1.push( 20 );
s1.push( 30 );
stack <int>::size_type i;
i = s1.size( );
cout << "The stack length is " << i << "." << endl;
i = s1.top( );
cout << "The element at the top of the stack is "
<< i << "." << endl;
}
The stack length is 3.
The element at the top of the stack is 30.
size
傳回堆疊中的項目數。
size_type size() const;
傳回值
堆疊目前的長度。
範例
// stack_size.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>
int main( )
{
using namespace std;
stack <int> s1, s2;
stack <int>::size_type i;
s1.push( 1 );
i = s1.size( );
cout << "The stack length is " << i << "." << endl;
s1.push( 2 );
i = s1.size( );
cout << "The stack length is now " << i << "." << endl;
}
The stack length is 1.
The stack length is now 2.
size_type
不帶正負號的整數類型,可以表示堆疊中的項目數。
typedef typename Container::size_type size_type;
備註
此類型與由堆疊所配接的基底容器 size_type 同義。
範例
如需如何宣告和使用 size_type的範例size,請參閱範例。
stack
建構空的堆疊,或是基底容器類別複本的堆疊。
stack();
explicit stack(const container_type& right);
參數
right
要從中複製所建構堆疊的容器。
範例
// stack_stack.cpp
// compile with: /EHsc
#include <stack>
#include <vector>
#include <list>
#include <iostream>
int main( )
{
using namespace std;
// Declares stack with default deque base container
stack <char> dsc1;
//Explicitly declares a stack with deque base container
stack <char, deque<char> > dsc2;
// Declares a stack with vector base containers
stack <int, vector<int> > vsi1;
// Declares a stack with list base container
stack <int, list<int> > lsi;
// The second member function copies elements from a container
vector<int> v1;
v1.push_back( 1 );
stack <int, vector<int> > vsi2( v1 );
cout << "The element at the top of stack vsi2 is "
<< vsi2.top( ) << "." << endl;
}
The element at the top of stack vsi2 is 1.
top
傳回堆疊頂端項目的參考。
reference top();
const_reference top() const;
傳回值
堆疊頂端之容器中最後一個項目的參考。
備註
堆疊不得為空,才能套用成員函式。 堆疊頂端是最近新增項目所佔用的位置,也是位於容器結尾的最後一個項目。
如果將 的 top 傳回值指派給 const_reference, stack 則無法修改 物件。 如果 的傳回值 top 指派給 reference, stack 則可以修改 物件。
範例
// stack_top.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>
int main( )
{
using namespace std;
stack <int> s1;
s1.push( 1 );
s1.push( 2 );
int& i = s1.top( );
const int& ii = s1.top( );
cout << "The top integer of the stack s1 is "
<< i << "." << endl;
i--;
cout << "The next integer down is "<< ii << "." << endl;
}
The top integer of the stack s1 is 2.
The next integer down is 1.
value_type
一個類型,代表堆疊中儲存為項目的物件類型。
typedef typename Container::value_type value_type;
備註
此類型與由堆疊所配接的基底容器 value_type 同義。
範例
// stack_value_type.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>
int main( )
{
using namespace std;
// Declares stacks with default deque base container
stack<int>::value_type AnInt;
AnInt = 69;
cout << "The value_type is AnInt = " << AnInt << endl;
stack<int> s1;
s1.push( AnInt );
cout << "The element at the top of the stack is "
<< s1.top( ) << "." << endl;
}
The value_type is AnInt = 69
The element at the top of the stack is 69.