stack
(STL/CLR)
範本類別描述一個物件,控制具有先出先出存取權之元素的不同長度序列。 您可以使用容器配接器 stack
來管理基礎容器作為向下推入堆疊。
在下列描述中,GValue
除非後者是 ref 類型,否則Value^
會是 Value
。 同樣地,GContainer
除非後者是 ref 型別,否則Container^
會是 Container
。
語法
template<typename Value,
typename Container>
ref class stack
: public
System::ICloneable,
Microsoft::VisualC::StlClr::IStack<GValue, GContainer>
{ ..... };
參數
Value
受控制序列中項目的類型。
Container
基礎容器的類型。
需求
標頭:<cliext/stack>
命名空間:cliext
宣告
類型定義 | 描述 |
---|---|
stack::const_reference |
項目的常數參考類型。 |
stack::container_type |
基礎容器的類型。 |
stack::difference_type |
兩個項目之間帶正負號距離的類型。 |
stack::generic_container |
容器配接器的泛型介面類型。 |
stack::generic_value |
容器配接器之泛型介面的專案型別。 |
stack::reference |
項目的參考類型。 |
stack::size_type |
兩個項目之間帶正負號距離的類型。 |
stack::value_type |
元素的類型。 |
成員函數 | 描述 |
---|---|
stack::assign |
取代所有項目。 |
stack::empty |
測試項目是否不存在。 |
stack::get_container |
存取基礎容器。 |
stack::pop |
拿掉最後一個專案。 |
stack::push |
加入新的最後一個專案。 |
stack::size |
計算元素的數目。 |
stack::stack |
建構容器物件。 |
stack::top |
存取最後一個項目。 |
stack::to_array |
將受控制序列複製到新的陣列。 |
屬性 | 說明 |
---|---|
stack::top_item |
存取最後一個項目。 |
Operator | 描述 |
---|---|
stack::operator= |
取代受控制的序列。 |
operator!= (堆疊) |
判斷物件是否 stack 不等於另一個 stack 物件。 |
operator< (堆疊) |
判斷物件是否 stack 小於另一個 stack 物件。 |
operator<= (堆疊) |
判斷物件是否 stack 小於或等於另一個 stack 物件。 |
operator== (堆疊) |
判斷物件是否 stack 等於另一個 stack 物件。 |
operator> (堆疊) |
判斷物件是否 stack 大於另一個 stack 物件。 |
operator>= (堆疊) |
判斷物件是否 stack 大於或等於另一個 stack 物件。 |
介面
介面 | 描述 |
---|---|
ICloneable | 複製物件。 |
IStack<Value, Container> |
維護泛型容器配接器。 |
備註
物件會透過類型的基礎容器來配置和釋放它所控制之序列的 Container
記憶體,該容器會儲存 Value
元素並視需要成長。 物件會限制只推送和彈出最後一個專案的存取,並實作最後一個輸出佇列(也稱為 LIFO 佇列或堆疊)。
成員
stack::assign
取代所有項目。
語法
void assign(stack<Value, Container>% right);
參數
right
要插入的容器配接器。
備註
成員函式會 right.get_container()
指派給基礎容器。 您可以使用它來變更堆疊的整個內容。
範例
// cliext_stack_assign.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign a repetition of values
Mystack c2;
c2.assign(c1);
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
a b c
stack::const_reference
項目的常數參考類型。
語法
typedef value_type% const_reference;
備註
此類型描述專案的常數參考。
範例
// cliext_stack_const_reference.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display reversed contents " c b a"
for (; !c1.empty(); c1.pop())
{ // get a const reference to an element
Mystack::const_reference cref = c1.top();
System::Console::Write("{0} ", cref);
}
System::Console::WriteLine();
return (0);
}
c b a
stack::container_type
基礎容器的類型。
語法
typedef Container value_type;
備註
此類型是範本參數 Container
的同義字。
範例
// cliext_stack_container_type.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c" using container_type
Mystack::container_type wc1 = c1.get_container();
for each (wchar_t elem in wc1)
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
stack::difference_type
兩個項目之間帶正負號距離的類型。
語法
typedef int difference_type;
備註
型別描述可能的負元素計數。
範例
// cliext_stack_difference_type.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// compute negative difference
Mystack::difference_type diff = c1.size();
c1.push(L'd');
c1.push(L'e');
diff -= c1.size();
System::Console::WriteLine("pushing 2 = {0}", diff);
// compute positive difference
diff = c1.size();
c1.pop();
c1.pop();
c1.pop();
diff -= c1.size();
System::Console::WriteLine("popping 3 = {0}", diff);
return (0);
}
a b c
pushing 2 = -2
popping 3 = 3
stack::empty
測試項目是否不存在。
語法
bool empty();
備註
成員函式會對空的受控制序列傳回 true。 相當於 size() == 0
。 您可以使用它來測試 是否 stack
為空白。
範例
// cliext_stack_empty.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("size() = {0}", c1.size());
System::Console::WriteLine("empty() = {0}", c1.empty());
// clear the container and reinspect
c1.pop();
c1.pop();
c1.pop();
System::Console::WriteLine("size() = {0}", c1.size());
System::Console::WriteLine("empty() = {0}", c1.empty());
return (0);
}
a b c
size() = 3
empty() = False
size() = 0
empty() = True
stack::generic_container
容器配接器的泛型介面類型。
語法
typedef Microsoft::VisualC::StlClr::IStack<Value>
generic_container;
備註
此類型描述此範本容器配接器類別的泛型介面。
範例
// cliext_stack_generic_container.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// get interface to container
Mystack::generic_container^ gc1 = %c1;
for each (wchar_t elem in gc1->get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// modify generic and display original
gc1->push(L'd');
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// modify original and display generic
c1.push(L'e');
for each (wchar_t elem in gc1->get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
a b c
a b c d
a b c d e
stack::generic_value
要與容器之泛型介面搭配使用的元素型別。
語法
typedef GValue generic_value;
備註
此類型描述 型 GValue
別的物件,該物件描述要與這個範本容器類別之泛型介面搭配使用的預存專案值。 (GValue
是 value_type
或 value_type^
如果 value_type
是 ref 類型。)
範例
// cliext_stack_generic_value.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// get interface to container
Mystack::generic_container^ gc1 = %c1;
for each (wchar_t elem in gc1->get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// display in reverse using generic_value
for (; !gc1->empty(); gc1->pop())
{
Mystack::generic_value elem = gc1->top();
System::Console::Write("{0} ", elem);
}
System::Console::WriteLine();
return (0);
}
a b c
a b c
c b a
stack::get_container
存取基礎容器。
語法
container_type^ get_container();
備註
成員函式會傳回基礎容器的句柄。 您可以使用它來略過容器包裝函式所施加的限制。
範例
// cliext_stack_get_container.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c" using container_type
Mystack::container_type wc1 = c1.get_container();
for each (wchar_t elem in wc1)
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
stack::operator=
取代受控制的序列。
語法
stack<Value, Container>% operator=(stack<Value, Container>% right);
參數
right
要複製的容器配接器。
備註
成員運算子會 right
複製到 物件,然後傳 *this
回 。 您使用它將受控制序列取代為 right
中受控制序列的複本。
範例
// cliext_stack_operator_as.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2 = c1;
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
a b c
stack::pop
拿掉最後一個專案。
語法
void pop();
備註
成員函式會移除受控制序列的最後一個專案,該元素必須是非空白的。 您可以使用它來縮短 stack
後面一個項目的 。
範例
// cliext_stack_pop.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// pop an element and redisplay
c1.pop();
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
a b
stack::push
加入新的最後一個專案。
語法
void push(value_type val);
備註
成員函式會在受控制序列結尾插入具有值 val
的專案。 您可以使用它將另一個專案附加至堆疊。
範例
// cliext_stack_push.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
stack::reference
項目的參考類型。
語法
typedef value_type% reference;
備註
此類型描述項目的參考。
範例
// cliext_stack_reference.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// modify top of stack and redisplay
Mystack::reference ref = c1.top();
ref = L'x';
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
a b x
stack::size
計算元素的數目。
語法
size_type size();
備註
成員函式會傳回受控制序列的長度。 您可以使用它來判斷目前在受控制序列中的元素數目。 如果您關心的只是序列是否具有非零大小,請參閱 stack::empty
。
範例
// cliext_stack_size.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("size() = {0} starting with 3", c1.size());
// pop an item and reinspect
c1.pop();
System::Console::WriteLine("size() = {0} after popping", c1.size());
// add two elements and reinspect
c1.push(L'a');
c1.push(L'b');
System::Console::WriteLine("size() = {0} after adding 2", c1.size());
return (0);
}
a b c
size() = 3 starting with 3
size() = 2 after popping
size() = 4 after adding 2
stack::size_type
兩個項目之間帶正負號距離的類型。
語法
typedef int size_type;
備註
此類型描述非負數項目計數。
範例
// cliext_stack_size_type.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// compute positive difference
Mystack::size_type diff = c1.size();
c1.pop();
c1.pop();
diff -= c1.size();
System::Console::WriteLine("size difference = {0}", diff);
return (0);
}
a b c
size difference = 2
stack::stack
建構容器配接器物件。
語法
stack();
stack(stack<Value, Container>% right);
stack(stack<Value, Container>^ right);
explicit stack(container_type% wrapped);
參數
right
要複製的物件。
wrapped
要使用的包裝容器。
備註
建構函式:
stack();
會建立空的包裝容器。 您可以使用它來指定空的初始控制序列。
建構函式:
stack(stack<Value, Container>% right);
會建立包裝的容器,該容器是 的 right.get_container()
複本。 您可以使用它來指定初始受控制序列,該序列是 物件 right
所stack
控制之序列的複本。
建構函式:
stack(stack<Value, Container>^ right);
會建立包裝的容器,該容器是 的 right->get_container()
複本。 您可以使用它來指定初始受控制序列,該序列是 物件 *right
所stack
控制之序列的複本。
建構函式:
explicit stack(container_type% wrapped);
使用現有的容器 wrapped
做為包裝容器。 您可以使用它從現有的容器建構 stack
。
範例
// cliext_stack_construct.cpp
// compile with: /clr
#include <cliext/stack>
#include <cliext/vector>
typedef cliext::stack<wchar_t> Mystack;
typedef cliext::vector<wchar_t> Myvector;
typedef cliext::stack<wchar_t, Myvector> Mystack_vec;
int main()
{
// construct an empty container
Mystack c1;
System::Console::WriteLine("size() = {0}", c1.size());
// construct from an underlying container
Myvector v2(5, L'x');
Mystack_vec c2(v2);
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// construct by copying another container
Mystack_vec c3(c2);
for each (wchar_t elem in c3.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// construct by copying another container through handle
Mystack_vec c4(%c2);
for each (wchar_t elem in c4.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
size() = 0
x x x x x
x x x x x
x x x x x
stack::to_array
將受控制序列複製到新的陣列。
語法
cli::array<Value>^ to_array();
備註
成員函式會傳回包含受控制序列的陣列。 您可以使用它,以陣列形式取得受控制序列的複本。
範例
// cliext_stack_to_array.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// copy the container and modify it
cli::array<wchar_t>^ a1 = c1.to_array();
c1.push(L'd');
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// display the earlier array copy
for each (wchar_t elem in a1)
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c d
a b c
stack::top
存取最後一個項目。
語法
reference top();
備註
成員函式會傳回受控制序列最後一個項目的參考,該元素必須是非空白的。 當您知道最後一個專案存在時,您可以使用它來存取最後一個專案。
範例
// cliext_stack_top.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// inspect last item
System::Console::WriteLine("top() = {0}", c1.top());
// alter last item and reinspect
c1.top() = L'x';
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
top() = c
a b x
stack::top_item
存取最後一個項目。
語法
property value_type top_item;
備註
屬性會存取受控制序列的最後一個專案,該元素必須是非空白的。 當您知道最後一個專案存在時,您可以使用它來讀取或寫入最後一個專案。
範例
// cliext_stack_top_item.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display initial contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// inspect last item
System::Console::WriteLine("top_item = {0}", c1.top_item);
// alter last item and reinspect
c1.top_item = L'x';
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
return (0);
}
a b c
top_item = c
a b x
stack::value_type
元素的類型。
語法
typedef Value value_type;
備註
此類型是範本參數 Value
的同義字。
範例
// cliext_stack_value_type.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display reversed contents " a b c" using value_type
for (; !c1.empty(); c1.pop())
{ // store element in value_type object
Mystack::value_type val = c1.top();
System::Console::Write("{0} ", val);
}
System::Console::WriteLine();
return (0);
}
c b a
operator!=
(堆疊)
Stack
不等於比較。
語法
template<typename Value,
typename Container>
bool operator!=(stack<Value, Container>% left,
stack<Value, Container>% right);
參數
left
要比較的左容器。
right
要比較的右容器。
備註
運算子函式會傳 !(left == right)
回 。 您可以使用它來測試是否 left
與依元素比較兩個堆疊時的順序不同 right
。
範例
// cliext_stack_operator_ne.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2.push(L'a');
c2.push(L'b');
c2.push(L'd');
// display contents " a b d"
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("[a b c] != [a b c] is {0}",
c1 != c1);
System::Console::WriteLine("[a b c] != [a b d] is {0}",
c1 != c2);
return (0);
}
a b c
a b d
[a b c] != [a b c] is False
[a b c] != [a b d] is True
operator<
(堆疊)
Stack
小於比較。
語法
template<typename Value,
typename Container>
bool operator<(stack<Value, Container>% left,
stack<Value, Container>% right);
參數
left
要比較的左容器。
right
要比較的右容器。
備註
如果 針對也是 true 的最低位置i
!(right[i] < left[i])
,運算子函式會傳回 trueleft[i] < right[i]
。 否則會傳回 left->size() < right->size()
。 您可以使用它來測試是否 left
在依 元素比較兩個堆疊之前 right
排序。
範例
// cliext_stack_operator_lt.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2.push(L'a');
c2.push(L'b');
c2.push(L'd');
// display contents " a b d"
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("[a b c] < [a b c] is {0}",
c1 < c1);
System::Console::WriteLine("[a b c] < [a b d] is {0}",
c1 < c2);
return (0);
}
a b c
a b d
[a b c] < [a b c] is False
[a b c] < [a b d] is True
operator<=
(堆疊)
Stack
小於或等於比較。
語法
template<typename Value,
typename Container>
bool operator<=(stack<Value, Container>% left,
stack<Value, Container>% right);
參數
left
要比較的左容器。
right
要比較的右容器。
備註
運算子函式會傳 !(right < left)
回 。 您可以使用它來測試當兩個堆疊依元素比較元素時,是否 left
未排序 right
。
範例
// cliext_stack_operator_le.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2.push(L'a');
c2.push(L'b');
c2.push(L'd');
// display contents " a b d"
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("[a b c] <= [a b c] is {0}",
c1 <= c1);
System::Console::WriteLine("[a b d] <= [a b c] is {0}",
c2 <= c1);
return (0);
}
a b c
a b d
[a b c] <= [a b c] is True
[a b d] <= [a b c] is False
operator==
(堆疊)
Stack
相等比較。
語法
template<typename Value,
typename Container>
bool operator==(stack<Value, Container>% left,
stack<Value, Container>% right);
參數
left
要比較的左容器。
right
要比較的右容器。
備註
運算子函式只有在 所left
控制的序列具有相同的長度,而且right
針對每個位置 i
left[i] == right[i]
、,傳回 true。 您可以使用它來測試是否 left
依元素比較兩個堆疊時排序的相同 right
。
範例
// cliext_stack_operator_eq.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2.push(L'a');
c2.push(L'b');
c2.push(L'd');
// display contents " a b d"
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("[a b c] == [a b c] is {0}",
c1 == c1);
System::Console::WriteLine("[a b c] == [a b d] is {0}",
c1 == c2);
return (0);
}
a b c
a b d
[a b c] == [a b c] is True
[a b c] == [a b d] is False
operator>
(堆疊)
Stack
大於比較。
語法
template<typename Value,
typename Container>
bool operator>(stack<Value, Container>% left,
stack<Value, Container>% right);
參數
left
要比較的左容器。
right
要比較的右容器。
備註
運算子函式會傳 right < left
回 。 您可以使用它來測試是否 left
依元素比較兩個堆疊之後 right
排序。
範例
// cliext_stack_operator_gt.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2.push(L'a');
c2.push(L'b');
c2.push(L'd');
// display contents " a b d"
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("[a b c] > [a b c] is {0}",
c1 > c1);
System::Console::WriteLine("[a b d] > [a b c] is {0}",
c2 > c1);
return (0);
}
a b c
a b d
[a b c] > [a b c] is False
[a b d] > [a b c] is True
operator>=
(堆疊)
Stack
大於或等於比較。
語法
template<typename Value,
typename Container>
bool operator>=(stack<Value, Container>% left,
stack<Value, Container>% right);
參數
left
要比較的左容器。
right
要比較的右容器。
備註
運算子函式會傳 !(left < right)
回 。 您可以使用它來測試兩 left
個堆疊是否依元素比較元素之前 right
未排序。
範例
// cliext_stack_operator_ge.cpp
// compile with: /clr
#include <cliext/stack>
typedef cliext::stack<wchar_t> Mystack;
int main()
{
Mystack c1;
c1.push(L'a');
c1.push(L'b');
c1.push(L'c');
// display contents " a b c"
for each (wchar_t elem in c1.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
// assign to a new container
Mystack c2;
c2.push(L'a');
c2.push(L'b');
c2.push(L'd');
// display contents " a b d"
for each (wchar_t elem in c2.get_container())
System::Console::Write("{0} ", elem);
System::Console::WriteLine();
System::Console::WriteLine("[a b c] >= [a b c] is {0}",
c1 >= c1);
System::Console::WriteLine("[a b c] >= [a b d] is {0}",
c1 >= c2);
return (0);
}
a b c
a b d
[a b c] >= [a b c] is True
[a b c] >= [a b d] is False