stack (STL/CLR)

此模板类描述了一个对象,该对象控制具有后进先出访问权限的变长元素序列。 使用容器适配器 stack 将基础容器作为下推堆栈进行管理。

在下面的描述中,GValueValue 相同,除非后者是 ref 类型,在这种情况下它是 Value^。 类似地,GContainerContainer 相同,除非后者是 ref 类型,在这种情况下它是 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 将受控序列复制到新数组。
properties 说明
stack::top_item 访问最后一个元素。
运算符 说明
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 类型的对象,该对象描述可与此模板容器类的泛型接口一起使用的存储元素值。 (如果 value_type 是 ref 类型,则 GValuevalue_typevalue_type^。)

示例

// 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() 副本的包装容器。 使用它来指定初始受控序列,该序列是由 stack 对象 right 控制的序列副本。

构造函数:

stack(stack<Value, Container>^ right);

创建一个作为 right->get_container() 副本的包装容器。 使用它来指定初始受控序列,该序列是由 stack 对象 *right 控制的序列副本。

构造函数:

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
要比较的右容器。

备注

如果对于 !(right[i] < left[i]) 的最低位置 ileft[i] < right[i] 也为 true,则运算符函数返回 true。 否则,它将返回 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
要比较的右容器。

备注

仅当 leftright 控制的序列具有相同的长度并且对于每个位置 ileft[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