list (STL/CLR)

此模板类描述了一个对象,该对象控制一个具有双向访问权限且长度可变的元素序列。 使用容器 list 将元素序列作为节点的双向链接列表进行管理,每个节点存储一个元素。

在下面的描述中,GValue 与 Value 相同,除非后者是 ref 类型,在这种情况下它是 Value^

语法

template<typename Value>
    ref class list
        :   public
        System::ICloneable,
        System::Collections::IEnumerable,
        System::Collections::ICollection,
        System::Collections::Generic::IEnumerable<GValue>,
        System::Collections::Generic::ICollection<GValue>,
        Microsoft::VisualC::StlClr::IList<GValue>
    { ..... };

参数


受控序列中的元素的类型。

要求

标头:<cliext/list>

命名空间:cliext

声明

类型定义 说明
list::const_iterator (STL/CLR) 受控序列的常量迭代器的类型。
list::const_reference (STL/CLR) 元素的常量引用的类型。
list::const_reverse_iterator (STL/CLR) 受控序列的常量反向迭代器的类型。
list::difference_type (STL/CLR) 两个元素间的带符号距离的类型。
list::generic_container (STL/CLR) 容器的泛型接口的类型。
list::generic_iterator (STL/CLR) 容器的泛型接口的迭代器的类型。
list::generic_reverse_iterator (STL/CLR) 容器的泛型接口的反向迭代器的类型。
list::generic_value (STL/CLR) 容器的泛型接口的元素类型。
list::iterator (STL/CLR) 受控序列的迭代器的类型。
list::reference (STL/CLR) 元素的引用的类型。
list::reverse_iterator (STL/CLR) 受控序列的反向迭代器的类型。
list::size_type (STL/CLR) 两个元素间的带符号距离的类型。
list::value_type (STL/CLR) 元素的类型。
成员函数 说明
list::assign (STL/CLR) 替换所有元素。
list::back (STL/CLR) 访问最后一个元素。
list::begin (STL/CLR) 指定受控序列的开头。
list::clear (STL/CLR) 删除所有元素。
list::empty (STL/CLR) 测试元素是否存在。
list::end (STL/CLR) 指定受控序列的末尾。
list::erase (STL/CLR) 移除指定位置处的元素。
list::front (STL/CLR) 访问第一个元素。
list::insert (STL/CLR) 在指定的位置添加元素。
list::list (STL/CLR) 构造容器对象。
list::merge (STL/CLR) 合并两个有序受控序列。
list::pop_back (STL/CLR) 删除最后一个元素。
list::pop_front (STL/CLR) 删除第一个元素。
list::push_back (STL/CLR) 添加新的最后一个元素。
list::push_front (STL/CLR) 添加新的第一个元素。
list::rbegin (STL/CLR) 指定反向受控序列的开头。
list::remove (STL/CLR) 删除具有指定值的元素。
list::remove_if (STL/CLR) 删除通过了指定测试的元素。
list::rend (STL/CLR) 指定反向受控序列的末尾。
list::resize (STL/CLR) 更改元素数目。
list::reverse (STL/CLR) 反转受控序列。
list::size (STL/CLR) 对元素数进行计数。
list::sort (STL/CLR) 对受控序列进行排序。
list::splice (STL/CLR) 重新联结节点间的链接。
list::swap (STL/CLR) 交换两个容器的内容。
list::to_array (STL/CLR) 将受控序列复制到新数组。
list::unique (STL/CLR) 删除通过了指定测试的相邻元素。
properties 说明
list::back_item (STL/CLR) 访问最后一个元素。
list::front_item (STL/CLR) 访问第一个元素。
操作员 说明
list::operator= (STL/CLR) 替换受控序列。
operator!= (list) (STL/CLR) 确定 list 对象是否不等于另一个 list 对象。
operator< (list) (STL/CLR) 确定 list 对象是否小于另一个 list 对象。
operator<= (list) (STL/CLR) 确定 list 对象是否小于或等于另一个 list 对象。
operator== (list) (STL/CLR) 确定 list 对象是否等于另一个 list 对象。
operator> (list) (STL/CLR) 确定 list 对象是否大于另一个 list 对象。
operator>= (list) (STL/CLR) 确定 list 对象是否大于或等于另一个 list 对象。

接口

接口 说明
ICloneable 复制对象。
IEnumerable 对元素进行排序。
ICollection 维护元素组。
IEnumerable<T> 对类型化元素进行排序。
ICollection<T> 维护类型化元素组。
IList<值> 维护泛型容器。

备注

对象为它作为双向链接列表中的单个节点控制的序列分配并释放存储。 它通过更改节点之间的链接来重新排列元素,而不是通过将一个节点的内容复制到另一个节点。 这意味着你可以自由插入和移除元素,不会干扰到其余的元素。 因此,list 非常适合模板类 queue (STL/CLR) 或模板类 stack (STL/CLR) 的基础容器。

list 对象支持双向迭代器,这意味着你可以通过指定受控序列中某个元素的迭代器来单步执行相邻元素。 有一个特殊的头节点对应于 list::end (STL/CLR)() 返回的迭代器。 可以递减此迭代器,以到达受控序列中的最后一个元素(如果存在)。 可以递增 list 迭代器以到达头节点,然后它与 end() 相等。 但你不能取消引用 end() 返回的迭代器。

请注意,不能在给定其数值位置的情况下直接引用 list 元素,这需要一个随机访问迭代器。 因此,list 无法用作模板类 priority_queue (STL/CLR) 的基础容器

list 迭代器将句柄存储到其关联的 list 节点,该节点又将句柄存储到其关联的容器。 只能将迭代器与其关联的容器对象一起使用。 只要关联的 list 节点与某些 list 关联,list 迭代器就保持有效。 此外,有效的迭代器是可取消引用的,只要所指定的元素值不等于 end(),你就可以使用该迭代器对其进行访问或更改。

擦除或移除元素会为其存储的值调用析构函数。 销毁容器会擦除所有元素。 因此,元素类型为 ref 类的容器可确保没有元素的生存期长于容器。 但是,请注意,句柄的容器不会销毁其元素

成员

list::assign (STL/CLR)

替换所有元素。

语法

void assign(size_type count, value_type val);
template<typename InIt>
    void assign(InIt first, InIt last);
void assign(System::Collections::Generic::IEnumerable<Value>^ right);

参数

count
要插入的元素数。

first
要插入的范围的开头。

last
要插入的范围的末尾。

right
要插入的枚举。

val
要插入的元素的值。

注解

第一个成员函数将受控序列替换为值 val 的 count 个重复元素。 使用它来填充具有相同值的元素的容器。

如果 InIt 是整数类型,则第二个成员函数的行为与 assign((size_type)first, (value_type)last) 相同。 否则,它将受控序列替换为序列 [first, last)。 使用它将受控序列设为另一个序列的副本。

第三个成员函数将受控序列替换为枚举器 right 指定的序列。 使用它将受控序列设为枚举器描述的序列的副本。

示例

// cliext_list_assign.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // assign a repetition of values
    cliext::list<wchar_t> c2;
    c2.assign(6, L'x');
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign an iterator range
    cliext::list<wchar_t>::iterator it = c1.end();
    c2.assign(c1.begin(), --it);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign an enumeration
    c2.assign(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<wchar_t>^)%c1);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
x x x x x x
a b
a b c

list::back (STL/CLR)

访问最后一个元素。

语法

reference back();

备注

成员函数返回对受控序列的最后一个元素的引用,该元素必须为非空元素。 当知道它存在时,可以使用它访问最后一个元素。

示例

// cliext_list_back.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect last item
    System::Console::WriteLine("back() = {0}", c1.back());

    // alter last item and reinspect
    c1.back() = L'x';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
back() = c
a b x

list::back_item (STL/CLR)

访问最后一个元素。

语法

property value_type back_item;

备注

该属性访问受控序列的最后一个元素,该元素必须为非空元素。 当知道它存在时,可以使用它读取或写入最后一个元素。

示例

// cliext_list_back_item.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect last item
    System::Console::WriteLine("back_item = {0}", c1.back_item);

    // alter last item and reinspect
    c1.back_item = L'x';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
back_item = c
a b x

list::begin (STL/CLR)

指定受控序列的开头。

语法

iterator begin();

备注

该成员函数返回一个随机访问迭代器,指定受控序列的第一个元素,或刚超出空序列末尾的位置。 用于获取一个迭代器,该迭代器指定受控序列的 current 开头,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

// cliext_list_begin.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items
    cliext::list<wchar_t>::iterator it = c1.begin();
    System::Console::WriteLine("*begin() = {0}", *it);
    System::Console::WriteLine("*++begin() = {0}", *++it);

    // alter first two items and reinspect
    *--it = L'x';
    *++it = L'y';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
*begin() = a
*++begin() = b
x y c

list::clear (STL/CLR)

删除所有元素。

语法

void clear();

备注

该成员函数有效地调用 list::erase (STL/CLR)(list::begin (STL/CLR)(),list::end (STL/CLR)())。 用于确保受控序列为空。

示例

// cliext_list_clear.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());

    // add elements and clear again
    c1.push_back(L'a');
    c1.push_back(L'b');

    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
    }
a b c
size() = 0
a b
size() = 0

list::const_iterator (STL/CLR)

受控序列的常量迭代器的类型。

语法

typedef T2 const_iterator;

备注

该类型描述了可充当受控序列的常量随机访问迭代器的未指定类型 T2 的对象。

示例

// cliext_list_const_iterator.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    cliext::list<wchar_t>::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        System::Console::Write("{0} ", *cit);
    System::Console::WriteLine();
    return (0);
    }
a b c

list::const_reference (STL/CLR)

元素的常量引用的类型。

语法

typedef value_type% const_reference;

备注

该类型描述了对元素的常量引用。

示例

// cliext_list_const_reference.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    cliext::list<wchar_t>::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        {   // get a const reference to an element
        cliext::list<wchar_t>::const_reference cref = *cit;
        System::Console::Write("{0} ", cref);
        }
    System::Console::WriteLine();
    return (0);
    }
a b c

list::const_reverse_iterator (STL/CLR)

受控序列的常量反向迭代器的类型。

语法

typedef T4 const_reverse_iterator;

备注

该类型描述了可充当受控序列的常量反向迭代器的未指定类型 T4 的对象。

示例

// cliext_list_const_reverse_iterator.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c" reversed
    cliext::list<wchar_t>::const_reverse_iterator crit = c1.rbegin();
    cliext::list<wchar_t>::const_reverse_iterator crend = c1.rend();
    for (; crit != crend; ++crit)
        System::Console::Write("{0} ", *crit);
    System::Console::WriteLine();
    return (0);
    }
c b a

list::difference_type (STL/CLR)

两个元素间的带符号距离的类型。

语法

typedef int difference_type;

备注

该类型描述有符号元素计数。

示例

// cliext_list_difference_type.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // compute positive difference
    cliext::list<wchar_t>::difference_type diff = 0;
    for (cliext::list<wchar_t>::iterator it = c1.begin();
        it != c1.end(); ++it) ++diff;
    System::Console::WriteLine("end()-begin() = {0}", diff);

    // compute negative difference
    diff = 0;
    for (cliext::list<wchar_t>::iterator it = c1.end();
        it != c1.begin(); --it) --diff;
    System::Console::WriteLine("begin()-end() = {0}", diff);
    return (0);
    }
a b c
end()-begin() = 3
begin()-end() = -3

list::empty (STL/CLR)

测试元素是否存在。

语法

bool empty();

备注

对于空受控序列,该成员函数返回 true。 它等效于 list::size (STL/CLR)() == 0。 用于测试 list 是否为空。

示例

// cliext_list_empty.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        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.clear();
    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

list::end (STL/CLR)

指定受控序列的末尾。

语法

iterator end();

备注

成员函数返回一个随机访问迭代器,它指向刚超出受控序列末尾的位置。 用于获取一个迭代器,该迭代器指定受控序列的末尾;如果受控序列的长度改变,其状态不会改变。

示例

// cliext_list_end.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect last two items
    cliext::list<wchar_t>::iterator it = c1.end();
    --it;
    System::Console::WriteLine("*-- --end() = {0}", *--it);
    System::Console::WriteLine("*--end() = {0}", *++it);

    // alter first two items and reinspect
    *--it = L'x';
    *++it = L'y';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
*-- --end() = b
*--end() = c
a x y

list::erase (STL/CLR)

移除指定位置处的元素。

语法

iterator erase(iterator where);
iterator erase(iterator first, iterator last);

参数

first
要擦除的范围的开头。

last
要擦除的范围的末尾。

where
要擦除的元素。

注解

第一个成员函数将删除 where 所指向的受控序列的元素。 用于删除单个元素。

第二个成员函数将移除范围 [firstlast) 中的受控序列的元素。 用于移除零个或多个连续元素。

这两个成员函数返回一个迭代器,该迭代器指定除任何已删除元素之外剩余的第一个元素,如果不存在这样的元素,则返回 list::end (STL/CLR)()

擦除元素时,元素副本的数量与擦除末尾和序列近端之间的元素数量成线性关系。 (当擦除序列任一端的一个或多个元素时,不会出现元素副本。)

示例

// cliext_list_erase.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // erase an element and reinspect
    System::Console::WriteLine("erase(begin()) = {0}",
        *c1.erase(c1.begin()));

    // add elements and display " b c d e"
    c1.push_back(L'd');
    c1.push_back(L'e');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // erase all but end
    cliext::list<wchar_t>::iterator it = c1.end();
    System::Console::WriteLine("erase(begin(), end()-1) = {0}",
        *c1.erase(c1.begin(), --it));
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
    }
a b c
erase(begin()) = b
b c d e
erase(begin(), end()-1) = e
size() = 1

list::front (STL/CLR)

访问第一个元素。

语法

reference front();

备注

成员函数返回对受控序列的第一个元素的引用,该元素必须为非空元素。 当知道它存在时,可以使用它读取或写入第一个元素。

示例

// cliext_list_front.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first item
    System::Console::WriteLine("front() = {0}", c1.front());

    // alter first item and reinspect
    c1.front() = L'x';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
front() = a
x b c

list::front_item (STL/CLR)

访问第一个元素。

语法

property value_type front_item;

备注

该属性访问受控序列的第一个元素,该元素必须为非空元素。 当知道它存在时,可以使用它读取或写入第一个元素。

示例

// cliext_list_front_item.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first item
    System::Console::WriteLine("front_item = {0}", c1.front_item);

    // alter first item and reinspect
    c1.front_item = L'x';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
front_item = a
x b c

list::generic_container (STL/CLR)

容器的泛型接口的类型。

语法

typedef Microsoft::VisualC::StlClr::
    IList<generic_value>
    generic_container;

备注

该类型描述此模板容器类的泛型接口。

示例

// cliext_list_generic_container.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    cliext::list<wchar_t>::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify generic and display original
    gc1->insert(gc1->end(), L'd');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify original and display generic
    c1.push_back(L'e');

    System::Collections::IEnumerator^ enum1 =
        gc1->GetEnumerator();
    while (enum1->MoveNext())
        System::Console::Write("{0} ", enum1->Current);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c
a b c d
a b c d e

list::generic_iterator (STL/CLR)

要与容器的泛型接口一起使用的迭代器的类型。

语法

typedef Microsoft::VisualC::StlClr::Generic::
    ContainerBidirectionalIterator<generic_value>
    generic_iterator;

备注

该类型描述可与此模板容器类的泛型接口一起使用的泛型迭代器。

示例

// cliext_list_generic_iterator.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    cliext::list<wchar_t>::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify generic and display original
    cliext::list<wchar_t>::generic_iterator gcit = gc1->begin();
    cliext::list<wchar_t>::generic_value gcval = *gcit;
    *++gcit = gcval;
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c
a a c

list::generic_reverse_iterator (STL/CLR)

要与容器的泛型接口一起使用的反向迭代器的类型。

语法

typedef Microsoft::VisualC::StlClr::Generic::
    ReverseBidirectionalIterator<generic_value> generic_reverse_iterator;

备注

该类型描述可与此模板容器类的泛型接口一起使用的泛型反向迭代器。

示例

// cliext_list_generic_reverse_iterator.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    cliext::list<wchar_t>::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify generic and display original
    cliext::list<wchar_t>::generic_reverse_iterator gcit = gc1->rbegin();
    cliext::list<wchar_t>::generic_value gcval = *gcit;
    *++gcit = gcval;
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c
a c c

list::generic_value (STL/CLR)

要与容器的泛型接口一起使用的元素的类型。

语法

typedef GValue generic_value;

备注

该类型描述一个 GValue 类型的对象,该对象描述可与此模板容器类的泛型接口一起使用的存储元素值。

示例

// cliext_list_generic_value.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    cliext::list<wchar_t>::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify generic and display original
    cliext::list<wchar_t>::generic_iterator gcit = gc1->begin();
    cliext::list<wchar_t>::generic_value gcval = *gcit;
    *++gcit = gcval;
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c
a a c

list::insert (STL/CLR)

在指定的位置添加元素。

语法

iterator insert(iterator where, value_type val);
void insert(iterator where, size_type count, value_type val);
template<typename InIt>
    void insert(iterator where, InIt first, InIt last);
void insert(iterator where,
    System::Collections::Generic::IEnumerable<Value>^ right);

参数

count
要插入的元素数。

first
要插入的范围的开头。

last
要插入的范围的末尾。

right
要插入的枚举。

val
要插入的元素的值。

where
容器中要在其前面插入的位置。

备注

每个成员函数紧跟受控序列中 where 指向的元素之前插入由剩余操作数指定的序列

第一个成员函数插入一个值为 val 的元素,并返回一个指定新插入的元素的迭代器。 使用它在迭代器指定的位置前面插入单个元素。

第二个成员函数插入值 val 的 count 个重复元素。 使用它来插入零个或多个连续元素,这些元素都是同一值的副本。

如果 InIt 是整数类型,则第三个成员函数的行为与 insert(where, (size_type)first, (value_type)last) 相同。 否则,它会插入序列 [first, last)。 使用它来插入从另一个序列复制的零个或多个连续元素。

第四个成员函数插入由 right 指定的序列。 用于插入枚举器所描述的序列。

插入单个元素时,元素副本的数量与插入点与序列近端之间的元素数量呈线性关系。 (在序列的任一端插入一个或多个元素时,不会出现元素副本。)如果 InIt 是输入迭代器,则第三个成员函数将有效地为序列中的每个元素执行一次插入。 否则,在插入 N 个元素时,元素副本的数量与 N 以及插入点和序列近端之间的元素数量呈线性关系。

示例

// cliext_list_insert.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert a single value using iterator
    cliext::list<wchar_t>::iterator it = c1.begin();
    System::Console::WriteLine("insert(begin()+1, L'x') = {0}",
        *c1.insert(++it, L'x'));
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert a repetition of values
    cliext::list<wchar_t> c2;
    c2.insert(c2.begin(), 2, L'y');
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert an iterator range
    it = c1.end();
    c2.insert(c2.end(), c1.begin(), --it);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert an enumeration
    c2.insert(c2.begin(),   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<wchar_t>^)%c1);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert a single value using index
    it = c2.begin();
    ++it, ++it, ++it;
    c2.insert(it, L'z');
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    return (0);
    }
a b c
insert(begin()+1, L'x') = x
a x b c
y y
y y a x b
a x b c y y a x b

list::iterator (STL/CLR)

受控序列的迭代器的类型。

语法

typedef T1 iterator;

备注

该类型描述了可充当受控序列的随机访问迭代器的未指定类型 T1 的对象。

示例

// cliext_list_iterator.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    cliext::list<wchar_t>::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        System::Console::Write("{0} ", *it);
    System::Console::WriteLine();

    // alter first element and redisplay
    it = c1.begin();
    *it = L'x';
    for (; it != c1.end(); ++it)
        System::Console::Write("{0} ", *it);
    System::Console::WriteLine();
    return (0);
    }
a b c
x b c

list::list (STL/CLR)

构造容器对象。

语法

list();
list(list<Value>% right);
list(list<Value>^ right);
explicit list(size_type count);
list(size_type count, value_type val);
template<typename InIt>
    list(InIt first, InIt last);
list(System::Collections::Generic::IEnumerable<Value>^ right);

参数

count
要插入的元素数。

first
要插入的范围的开头。

last
要插入的范围的末尾。

right
要插入的对象或范围。

val
要插入的元素的值。

注解

构造函数:

list();

初始化没有元素的受控序列。 用于指定空的初始受控序列。

构造函数:

list(list<Value>% right);

使用序列 [right.begin(), right.end()) 初始化受控序列。 使用它来指定初始受控序列,该序列是由列表对象 right 控制的序列副本

构造函数:

list(list<Value>^ right);

使用序列 [right->begin(), right->end()) 初始化受控序列。 使用它来指定初始受控序列,该序列是由句柄为 right 的向量对象控制的序列副本

构造函数:

explicit list(size_type count);

使用 count 个元素初始化受控序列,每个元素的值为 value_type()。 使用它来填充具有默认值的元素的容器。

构造函数:

list(size_type count, value_type val);

使用 count 个元素初始化受控序列,每个元素的值为 val。 使用它来填充具有相同值的元素的容器。

构造函数:

template<typename InIt>

list(InIt first, InIt last);

使用序列 [first, last) 初始化受控序列。 使用它将受控序列设为另一个序列的副本。

构造函数:

list(System::Collections::Generic::IEnumerable<Value>^ right);

使用枚举器 right 指定的序列初始化受控序列。 使用它将受控序列设为枚举器描述的另一个序列的副本。

示例

// cliext_list_construct.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
// construct an empty container
    cliext::list<wchar_t> c1;
    System::Console::WriteLine("size() = {0}", c1.size());

    // construct with a repetition of default values
    cliext::list<wchar_t> c2(3);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", (int)elem);
    System::Console::WriteLine();

    // construct with a repetition of values
    cliext::list<wchar_t> c3(6, L'x');
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct with an iterator range
    cliext::list<wchar_t>::iterator it = c3.end();
    cliext::list<wchar_t> c4(c3.begin(), --it);
    for each (wchar_t elem in c4)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct with an enumeration
    cliext::list<wchar_t> c5(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<wchar_t>^)%c3);
    for each (wchar_t elem in c5)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying another container
    cliext::list<wchar_t> c7(c3);
    for each (wchar_t elem in c7)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying a container handle
    cliext::list<wchar_t> c8(%c3);
    for each (wchar_t elem in c8)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    return (0);
    }
size() = 0
0 0 0
x x x x x x
x x x x x
x x x x x x
x x x x x x
x x x x x x

list::merge (STL/CLR)

合并两个有序受控序列。

语法

void merge(list<Value>% right);
template<typename Pred2>
    void merge(list<Value>% right, Pred2 pred);

参数

pred
元素对的比较器。

right
要合并的容器。

备注

第一个成员函数从由 right 控制的序列中删除所有元素,并将其插入到受控序列中。 这两个序列之前都必须按 operator< 排序 - 元素的值不能随着顺序的进展而减少。 生成的序列也按 operator< 排序。 可以使用此成员函数将值递增的两个序列合并为值递增的一个序列。

第二个成员函数的行为与第一个成员函数相同,只不过序列按 pred 排序 -- 对于序列中元素 Y 后面的任何元素 Xpred(X, Y) 必须为 false。 可以使用它合并由指定的谓词函数或委托排序的两个序列。

这两个函数都执行稳定的合并 - 原始受控序列中的任何一对元素在生成的受控序列中都不会反转。 同样,如果所生成的受控序列中的一对元素 XY 具有相同的顺序 -- !(X < Y) && !(X < Y) -- 原始受控序列中的元素会出现在由 right 控制的序列中的元素之前

示例

// cliext_list_merge.cpp
// compile with: /clr
#include <cliext/list>

typedef cliext::list<wchar_t> Mylist;
int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'c');
    c1.push_back(L'e');

    // display initial contents " a c e"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    cliext::list<wchar_t> c2;
    c2.push_back(L'b');
    c2.push_back(L'd');
    c2.push_back(L'f');

    // display initial contents " b d f"
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // merge and display
    cliext::list<wchar_t> c3(c1);
    c3.merge(c2);
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine("c2.size() = {0}", c2.size());

    // sort descending, merge descending, and redisplay
    c1.sort(cliext::greater<wchar_t>());
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    c3.sort(cliext::greater<wchar_t>());
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    c3.merge(c1, cliext::greater<wchar_t>());
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine("c1.size() = {0}", c1.size());
    return (0);
    }
a c e
b d f
a b c d e f
c2.size() = 0
e c a
f e d c b a
f e e d c c b a a
c1.size() = 0

list::operator= (STL/CLR)

替换受控序列。

语法

list<Value>% operator=(list<Value>% right);

参数

right
用于复制的容器。

注解

成员运算符将 right 复制到对象,然后返回 *this。 用于将受控序列替换为 right 中的受控序列的副本

示例

// cliext_list_operator_as.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2 = c1;
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c

list::pop_back (STL/CLR)

删除最后一个元素。

语法

void pop_back();

备注

成员函数删除受控序列的最后一个元素,该元素必须为非空元素。 用于缩短列表后面的一个元素。

示例

// cliext_list_pop_back.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // pop an element and redisplay
    c1.pop_back();
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b

list::pop_front (STL/CLR)

删除第一个元素。

语法

void pop_front();

备注

成员函数删除受控序列的第一个元素,该元素必须为非空元素。 用于缩短列表前面的一个元素。

示例

// cliext_list_pop_front.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // pop an element and redisplay
    c1.pop_front();
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
b c

list::push_back (STL/CLR)

添加新的最后一个元素。

语法

void push_back(value_type val);

备注

此成员函数在受控序列的末尾插入一个值为 val 的元素。 可用于将另一个元素追加到列表中。

示例

// cliext_list_push_back.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c

list::push_front (STL/CLR)

添加新的第一个元素。

语法

void push_front(value_type val);

备注

此成员函数在受控序列的开头插入一个值为 val 的元素。 可用于将另一个元素追加到列表中。

示例

// cliext_list_push_front.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_front(L'a');
    c1.push_front(L'b');
    c1.push_front(L'c');

    // display contents " c b a"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
c b a

list::rbegin (STL/CLR)

指定反向受控序列的开头。

语法

reverse_iterator rbegin();

备注

该成员函数返回一个反向迭代器,指定受控序列的最后一个元素,或刚超出空序列开头的位置。 因此,它指定反向序列的 beginning。 用于获取一个迭代器,该迭代器指定按相反顺序显示的受控序列的 current 开头,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

// cliext_list_rbegin.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items in reversed sequence
    cliext::list<wchar_t>::reverse_iterator rit = c1.rbegin();
    System::Console::WriteLine("*rbegin() = {0}", *rit);
    System::Console::WriteLine("*++rbegin() = {0}", *++rit);

    // alter first two items and reinspect
    *--rit = L'x';
    *++rit = L'y';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
*rbegin() = c
*++rbegin() = b
a y x

list::reference (STL/CLR)

元素的引用的类型。

语法

typedef value_type% reference;

备注

该类型描述了对元素的引用。

示例

// cliext_list_reference.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    cliext::list<wchar_t>::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        {   // get a reference to an element
        cliext::list<wchar_t>::reference ref = *it;
        System::Console::Write("{0} ", ref);
        }
    System::Console::WriteLine();

    // modify contents " a b c"
    for (it = c1.begin(); it != c1.end(); ++it)
        {   // get a reference to an element
        cliext::list<wchar_t>::reference ref = *it;

        ref += (wchar_t)(L'A' - L'a');
        System::Console::Write("{0} ", ref);
        }
    System::Console::WriteLine();
    return (0);
    }
a b c
A B C

list::remove (STL/CLR)

删除具有指定值的元素。

语法

void remove(value_type val);

参数

val
要删除的元素的值。

备注

成员函数删除受控序列中 ((System::Object^)val)->Equals((System::Object^)x) 为 true的元素(如果有)。 使用它清除具有指定值的任意元素。

示例

// cliext_list_remove.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // fail to remove and redisplay
    c1.remove(L'A');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // remove and redisplay
    c1.remove(L'b');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c
a c

list::remove_if (STL/CLR)

删除通过了指定测试的元素。

语法

template<typename Pred1>
    void remove_if(Pred1 pred);

参数

pred
测试要删除的元素。

备注

成员函数从受控序列中删除(清除)所有 pred(X) 为 true 的元素 X。 使用它删除满足指定为函数或委托的条件的所有元素。

示例

// cliext_list_remove_if.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'b');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b b b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // fail to remove and redisplay
    c1.remove_if(cliext::binder2nd<cliext::equal_to<wchar_t> >(
        cliext::equal_to<wchar_t>(), L'd'));
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // remove and redisplay
    c1.remove_if(cliext::binder2nd<cliext::not_equal_to<wchar_t> >(
        cliext::not_equal_to<wchar_t>(), L'b'));
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b b b c
a b b b c
b b b

list::rend (STL/CLR)

指定反向受控序列的末尾。

语法

reverse_iterator rend();

备注

该成员函数返回一个反向迭代器,指向刚超出受控序列开头的位置。 因此,它指定反向序列的 end。 用于获取一个迭代器,该迭代器指定按相反顺序显示的受控序列的 current 末尾,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

// cliext_list_rend.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items
    cliext::list<wchar_t>::reverse_iterator rit = c1.rend();
    --rit;
    System::Console::WriteLine("*-- --rend() = {0}", *--rit);
    System::Console::WriteLine("*--rend() = {0}", *++rit);

    // alter first two items and reinspect
    *--rit = L'x';
    *++rit = L'y';
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
*-- --rend() = b
*--rend() = a
y x c

list::resize (STL/CLR)

更改元素数目。

语法

void resize(size_type new_size);
void resize(size_type new_size, value_type val);

参数

new_size
受控序列的新大小。

val
填充元素的值。

注解

这两个成员函数都确保 list::size (STL/CLR)() 以后返回 new_size。 如果它必须使受控序列更长,则第一个成员函数追加具有值 value_type() 的元素,而第二个成员函数追加具有值 val 的元素。 为了缩短受控序列,这两个成员函数都会有效地擦除最后一个元素 list::size (STL/CLR)() -new_size 次。 使用它来确保受控序列的大小为 new_size,方法是剪裁或填充当前受控序列

示例

// cliext_list_resize.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
// construct an empty container and pad with default values
    cliext::list<wchar_t> c1;
    System::Console::WriteLine("size() = {0}", c1.size());
    c1.resize(4);
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", (int)elem);
    System::Console::WriteLine();

    // resize to empty
    c1.resize(0);
    System::Console::WriteLine("size() = {0}", c1.size());

    // resize and pad
    c1.resize(5, L'x');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
size() = 0
0 0 0 0
size() = 0
x x x x x

list::reverse (STL/CLR)

反转受控序列。

语法

void reverse();

备注

成员函数反转受控序列中所有元素的顺序。 使用它来反映元素列表。

示例

// cliext_list_reverse.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // reverse and redisplay
    c1.reverse();
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
c b a

list::reverse_iterator (STL/CLR)

受控序列的反向迭代器的类型。

语法

typedef T3 reverse_iterator;

备注

该类型描述了可充当受控序列的反向迭代器的未指定类型 T3 的对象。

示例

// cliext_list_reverse_iterator.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c" reversed
    cliext::list<wchar_t>::reverse_iterator rit = c1.rbegin();
    for (; rit != c1.rend(); ++rit)
        System::Console::Write("{0} ", *rit);
    System::Console::WriteLine();

    // alter first element and redisplay
    rit = c1.rbegin();
    *rit = L'x';
    for (; rit != c1.rend(); ++rit)
        System::Console::Write("{0} ", *rit);
    System::Console::WriteLine();
    return (0);
    }
c b a
x b a

list::size (STL/CLR)

对元素数进行计数。

语法

size_type size();

备注

成员函数将返回受控序列的长度。 用于确定受控序列中的当前元素数。 如果只关心序列是否具有非零大小,请参阅 list::empty (STL/CLR)()

示例

// cliext_list_size.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine("size() = {0} starting with 3", c1.size());

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0} after clearing", c1.size());

    // add elements and clear again
    c1.push_back(L'a');
    c1.push_back(L'b');
    System::Console::WriteLine("size() = {0} after adding 2", c1.size());
    return (0);
    }
a b c
size() = 3 starting with 3
size() = 0 after clearing
size() = 2 after adding 2

list::size_type (STL/CLR)

两个元素间的带符号距离的类型。

语法

typedef int size_type;

备注

该类型描述非负元素计数。

示例

// cliext_list_size_type.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // compute positive difference
    cliext::list<wchar_t>::size_type diff = 0;
    for (cliext::list<wchar_t>::iterator it = c1.begin();
        it != c1.end(); ++it)
        ++diff;
    System::Console::WriteLine("end()-begin() = {0}", diff);
    return (0);
    }
a b c
end()-begin() = 3

list::sort (STL/CLR)

对受控序列进行排序。

语法

void sort();
template<typename Pred2>
    void sort(Pred2 pred);

参数

pred
元素对的比较器。

备注

第一个成员函数重新排列受控序列中的元素,使它们按 operator< 排序 -- 元素的值不会随着序列的进展而减少。 使用此成员函数按递增顺序对序列进行排序。

第二个成员函数的行为与第一个成员函数相同,只不过序列按 pred 排序 -- 对于最终序列中元素 Y 后面的任何元素 Xpred(X, Y) 为 false。 可以使用它按谓词函数或委托指定的顺序对序列进行排序。

这两个函数都执行稳定的排序 - 原始受控序列中的元素对在生成的受控序列中不会反转。

示例

// cliext_list_sort.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // sort descending and redisplay
    c1.sort(cliext::greater<wchar_t>());
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // sort ascending and redisplay
    c1.sort();
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
c b a
a b c

list::splice (STL/CLR)

重新联结节点间的链接。

语法

void splice(iterator where, list<Value>% right);
void splice(iterator where, list<Value>% right,
    iterator first);
void splice(iterator where, list<Value>% right,
    iterator first, iterator last);

参数

first
要接合的范围的开始。

last
要接合的范围的末尾。

right
要从中接合的容器。

where
容器中要在其前面接合的位置。

备注

第一对成员函数将由 right 控制的序列正好插入 where 所指向的控制序列中的元素之前。 它还会删除 right 中的所有元素。 (%right 不得等于 this。)列表中的所有内容接合到另一个列表中。

第二个成员函数删除由 right 控制的序列中 first 指向的元素,并将其正好插入到 where 所指向的控制序列中的元素之前。 (如果 where==first||where== ++first,则不会发生任何更改。)使用它将一个列表中的单个元素接合到另一个列表中。

第三个成员函数将由 right 控制的序列中的 [first, last) 指定的子范围正好插入 where 所指向的控制序列中的元素之前。 它还会删除由 right 控制的序列中的原始子范围。 (如果 right == this,则范围 [first, last) 不得包含 Where 所指向的元素。) 可以使用它将一个列表中的零个或多个元素的子序列接合到另一个列表中。

示例

// cliext_list_splice.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // splice to a new list
    cliext::list<wchar_t> c2;
    c2.splice(c2.begin(), c1);
    System::Console::WriteLine("c1.size() = {0}", c1.size());
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // return one element
    c1.splice(c1.end(), c2, c2.begin());
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // return remaining elements
    c1.splice(c1.begin(), c2, c2.begin(), c2.end());
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine("c2.size() = {0}", c2.size());
    return (0);
    }
a b c
c1.size() = 0
a b c
a
b c
b c a
c2.size() = 0

list::swap (STL/CLR)

交换两个容器的内容。

语法

void swap(list<Value>% right);

参数

right
要与其交换内容的容器。

注解

成员函数交换 *this 和 right 之间的受控序列。 它在常量时间内执行此操作且不引发异常。 你可以将其用作交换两个容器的内容的快捷方式。

示例

// cliext_list_swap.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct another container with repetition of values
    cliext::list<wchar_t> c2(5, L'x');
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // swap and redisplay
    c1.swap(c2);
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
x x x x x
x x x x x
a b c

list::to_array (STL/CLR)

将受控序列复制到新数组。

语法

cli::array<Value>^ to_array();

备注

成员函数返回一个包含受控序列的数组。 用于以数组形式获取受控序列的副本。

示例

// cliext_list_to_array.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // copy the container and modify it
    cli::array<wchar_t>^ a1 = c1.to_array();

    c1.push_back(L'd');
    for each (wchar_t elem in c1)
        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

list::unique (STL/CLR)

删除通过了指定测试的相邻元素。

语法

void unique();
template<typename Pred2>
    void unique(Pred2 pred);

参数

pred
元素对的比较器。

备注

第一个成员函数从受控序列删除(清除)经比较等于其前一个元素的每个元素 - 如果元素 X 在元素 YX == Y 之前,则成员函数删除 Y。 可以使用它删除比较相等的相邻元素子序列的所有副本,只保留一个副本。 请注意,如果受控序列按顺序排序,例如通过调用 list::sort (STL/CLR)(),则成员函数仅保留具有唯一值的元素。 (由此而得名)。

第二个成员函数的行为与第一个成员函数相同,只不过它删除了元素 X 后面的每个元素 Y,而该元素对应于 pred(X, Y)。 可以使用它删除满足指定的谓词函数或委托的相邻元素的每个子序列的所有副本,只保留一个副本。 请注意,如果受控序列按顺序排序(例如通过调用 sort(pred)),则成员函数只保留与其他元素顺序不同的元素。

示例

// cliext_list_unique.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display initial contents " a a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // display contents after unique
    cliext::list<wchar_t> c2(c1);
    c2.unique();
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // display contents after unique(not_equal_to)
    c2 = c1;
    c2.unique(cliext::not_equal_to<wchar_t>());
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a a b c
a b c
a a

list::value_type (STL/CLR)

元素的类型。

语法

typedef Value value_type;

备注

该类型是模板参数 Value 的同义词

示例

// cliext_list_value_type.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c" using value_type
    for (cliext::list<wchar_t>::iterator it = c1.begin();
        it != c1.end(); ++it)
        {   // store element in value_type object
        cliext::list<wchar_t>::value_type val = *it;

        System::Console::Write("{0} ", val);
        }
    System::Console::WriteLine();
    return (0);
    }
a b c

operator!= (list) (STL/CLR)

列表不等于比较。

语法

template<typename Value>
    bool operator!=(list<Value>% left,
        list<Value>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

注解

运算符函数返回 !(left == right)。 当两个列表按元素逐个比较时,你可以使用它来测试 left 的顺序是否与 right 不同

示例

// cliext_list_operator_ne.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2.push_back(L'a');
    c2.push_back(L'b');
    c2.push_back(L'd');

    // display contents " a b d"
    for each (wchar_t elem in c2)
        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< (list) (STL/CLR)

列表小于比较。

语法

template<typename Value>
    bool operator<(list<Value>% left,
        list<Value>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

如果对于 !(right[i] < left[i]) 的最低位置 ileft[i] < right[i] 也为 true,则运算符函数返回 true。 否则,它返回 left->size() < right->size()。当两个列表按元素逐个比较时,可以用它来测试 left 是否排在 right 之前

示例

// cliext_list_operator_lt.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2.push_back(L'a');
    c2.push_back(L'b');
    c2.push_back(L'd');

    // display contents " a b d"
    for each (wchar_t elem in c2)
        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<= (list) (STL/CLR)

列表小于或等于比较。

语法

template<typename Value>
    bool operator<=(list<Value>% left,
        list<Value>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 !(right < left)。 当两个列表按元素逐个比较时,你可以使用它来测试 left 是否没有排在 right 之后

示例

// cliext_list_operator_le.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2.push_back(L'a');
    c2.push_back(L'b');
    c2.push_back(L'd');

    // display contents " a b d"
    for each (wchar_t elem in c2)
        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== (list) (STL/CLR)

列表等于比较。

语法

template<typename Value>
    bool operator==(list<Value>% left,
        list<Value>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

注解

仅当 left 和 right 控制的序列具有相同的长度并且对于每个位置 ileft[i] ==right[i] 时,运算符函数才返回 true。 当两个列表按元素逐个比较时,你可以使用它来测试 left 的顺序是否与 right 相同

示例

// cliext_list_operator_eq.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2.push_back(L'a');
    c2.push_back(L'b');
    c2.push_back(L'd');

    // display contents " a b d"
    for each (wchar_t elem in c2)
        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> (list) (STL/CLR)

列表大于比较。

语法

template<typename Value>
    bool operator>(list<Value>% left,
        list<Value>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 right<left。 当两个列表按元素逐个比较时,你可以使用它来测试 left 是否排在 right 之后

示例

// cliext_list_operator_gt.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2.push_back(L'a');
    c2.push_back(L'b');
    c2.push_back(L'd');

    // display contents " a b d"
    for each (wchar_t elem in c2)
        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>= (list) (STL/CLR)

列表大于或等于比较。

语法

template<typename Value>
    bool operator>=(list<Value>% left,
        list<Value>% right);

参数

left
要比较的左容器。

right
要比较的右容器。

备注

运算符函数返回 !(left<right)。 当两个列表按元素逐个比较时,你可以使用它来测试 left 是否没有排在 right 之前

示例

// cliext_list_operator_ge.cpp
// compile with: /clr
#include <cliext/list>

int main()
    {
    cliext::list<wchar_t> c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    cliext::list<wchar_t> c2;
    c2.push_back(L'a');
    c2.push_back(L'b');
    c2.push_back(L'd');

    // display contents " a b d"
    for each (wchar_t elem in c2)
        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