list容器的使用方法


本文出自:https://msdn.microsoft.com/zh-cn/library/802d66bt.aspx


list 类

Visual Studio 2015
 

若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 2017 RC 文档

STL 列表类是序列容器的一个模板类,用于将它们的元素保持为线性排列,并允许在序列的任何位置高效插入和删除。 序列存储为双向链接列表的元素,每个都包含某种类型的成员类型

C++
template <class Type, class Allocator= allocator<Type>>  
class list  

参数

类型
要存储在列表中的元素数据类型。

Allocator
表示所存储分配器对象的类型,该分配器对象封装有关列表的内存分配和解除分配的详细信息。 此参数是可选的而默认值是分配器类型

容器类型选择通常应根据应用程序所需的搜索和插入的类型。 当对任何元素的随机访问超出限制并且仅要求在序列的末尾插入或删除元素时,矢量应作为用于管理序列的首选容器。 当需要随机访问并且在序列起始处和末尾处插入和删除元素已到达极限时,应首选类 deque 容器进行操作。

列表成员函数合并反向唯一删除,和remove_if经过优化的 list 对象上操作,并提供它们对应的泛型的高性能替代方法。

当成员函数必须插入或删除列表中的元素时,将发生列表的重新分配。 在所有这类情况下,仅指向受控制序列被消除部分的迭代器或引用将变为无效。

包括 STL 标准标头<>>定义容器模板类列表和几个支持模板。

构造函数

   
列表 构造一个列表,它具有特定大小或它的元素具有特定值,或具有特定 allocator 或作为某个其他列表副本。

Typedef

   
allocator_type 表示列表对象的 allocator 类的类型。
const_iterator 提供可读取列表中 const 元素的双向迭代器的类型。
const_pointer 提供指向列表中 const 元素的指针的类型。
const_reference 提供对存储于列表中供读取和执行 const 操作的 const 元素的引用的类型。
const_reverse_iterator 提供可读取列表中任何 const 元素的双向迭代器的类型。
difference_type 提供引用同一列表中的元素的两个迭代器之间的差异的类型。
迭代器 提供可读取或修改列表中任何元素的双向迭代器的类型。
指针 提供指向列表中元素的指针的类型。
引用 提供对存储于列表中供读取和执行 const 操作的 const 元素的引用的类型。
reverse_iterator 提供可读取或修改反向列表中的元素的双向迭代器的类型。
size_type 计算列表中元素的数目的类型。
value_type 表示列表中存储的数据类型的类型。

成员函数

   
分配 将元素从列表中擦除并将一组新的元素复制到目标列表。
返回 返回对列表中最后一个元素的引用。
开始 返回发现列表中第一个元素的位置的迭代器。
list::cbegin 返回发现列表中第一个元素的位置的常量迭代器。
list::cend 返回发现一个列表中最后一个元素之后的位置的敞亮表达式。
list:: clear 消除列表中的全部元素。
list::crbegin 返回发现反向列表中第一个元素的位置的常量迭代器。
list::crend 返回用于发现反向列表中最后一个元素之后的位置的常量迭代器。
list::emplace 将构造的元素插入到列表中的指定位置。
list::emplace_back 在列表的结尾处添加一个就地构造的元素。
list::emplace_front 在列表的起始位置添加一个就地构造的元素。
为空 测试列表是否为空。
结束 返回用于发现列表中最后一个元素之后的位置的迭代器。
擦除 从列表中的指定位置移除一个或一系列元素。
前端 返回对列表中第一个元素的引用。
get_allocator 返回用于构造列表的 allocator 对象的一个副本。
插入 将一个、几个或一系列元素插入列表中的指定位置。
max_size 返回列表的最大长度。
合并 将元素从参数列表移除,将它们插入目标列表,将新的组合元素集以升序或其他指定顺序排序。
pop_back 删除列表末尾的元素。
pop_front 删除列表起始处的一个元素。
push_back 在列表的末尾添加元素。
push_front 在列表的开头添加元素。
rbegin 返回发现反向列表中第一个元素的位置的迭代器。
remove 清除列表中与指定值匹配的元素。
remove_if 将满足指定谓词的元素从列表中消除。
rend 返回发现反向列表中最后一个元素之后的位置的迭代器。
调整大小 为列表指定新的大小。
反向 反转列表中元素的顺序。
大小 返回列表中元素的数目。
排序 按升序或其他顺序关系排列列表中的元素。
拼接 将元素从自变量列表中删除或将它们插入目标列表。
交换 交换两个列表的元素。
唯一 从列表中删除满足某些其他二元谓词的相邻重复元素或相邻元素。

运算符

   
list::operator = 用另一个列表的副本替换列表中的元素。

标头:<>>

表示列表对象的分配器类的类型。

typedef Allocator allocator_type;  

备注

allocator_type是模板参数的同义词分配器。

示例

请参阅示例get_allocator

清除列表中的元素,并将一组新元素复制到目标列表。

void assign(
    size_type Count,  
    const Type& Val);

void assign  
    initializer_list<Type> IList);

template <class InputIterator>  
void assign(
    InputIterator First,  
    InputIterator Last);

参数

First
要从自变量列表中复制的一系列元素中的第一个元素的位置。

Last
超出要从参数列表中复制的一系列元素的范围的第一个元素的位置。

Count
要插入列表中的元素副本的数目。

Val
要插入到列表中的元素的值。

IList
包含要插入的元素的 initializer_list。

备注

清除目标列表中的任何现有元素后,将原始列表或其他列表中的一系列指定的元素插入目标列表中,或将指定值的新元素的副本插入目标列表中

示例

// list_assign.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    list<int> c1, c2;  
    list<int>::const_iterator cIter;  
  
    c1.push_back(10);  
    c1.push_back(20);  
    c1.push_back(30);  
    c2.push_back(40);  
    c2.push_back(50);  
    c2.push_back(60);  
  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    c1.assign(++c2.begin(), c2.end());  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    c1.assign(7, 4);  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    c1.assign({ 10, 20, 30, 40 });  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
}  

c1 = 10 20 30c1 = 50 60c1 = 4 4 4 4 4 4 4c1 = 10 20 30 40  

返回对列表中最后一个元素的引用。

reference back();

const_reference back() const;

返回值

列表的最后一个元素。 如果列表为空,则返回值不确定。

备注

如果返回值为分配给const_reference,不能修改列表对象。 如果返回值为分配给引用,可以修改列表对象。

通过使用在编译时_ITERATOR_DEBUG_LEVEL定义为 1 或 2,运行时将发生错误如果试图访问空列表中的元素。 有关更多信息,请参见 Checked Iterators 。

示例

// list_back.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
   c1.push_back( 11 );  
  
   int& i = c1.back( );  
   const int& ii = c1.front( );  
  
   cout << "The last integer of c1 is " << i << endl;  
   i--;  
   cout << "The next-to-last integer of c1 is " << ii << endl;  
}  

The last integer of c1 is 11  
The next-to-last integer of c1 is 10  

返回发现列表中第一个元素的位置的迭代器。

const_iterator begin() const;

iterator begin();

返回值

发现列表中第一个元素的位置或空列表之后的位置的双向迭代器。

备注

如果返回值为开始分配给const_iterator,不能修改列表对象中的元素。 如果返回值为开始分配给迭代器,可以修改列表对象中的元素。

示例

// list_begin.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter;  
   list <int>::const_iterator c1_cIter;  
  
   c1.push_back( 1 );  
   c1.push_back( 2 );  
  
   c1_Iter = c1.begin( );  
   cout << "The first element of c1 is " << *c1_Iter << endl;  
  
 *c1_Iter = 20;  
   c1_Iter = c1.begin( );  
   cout << "The first element of c1 is now " << *c1_Iter << endl;  
  
   // The following line would be an error because iterator is const  
   // *c1_cIter = 200;  
}  

The first element of c1 is 1  
The first element of c1 is now 20  

返回确定范围中第一个元素地址的 const 迭代器。

const_iterator cbegin() const;

返回值

const 双向访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。

备注

由于使用 cbegin 的返回值,因此不能修改范围中的元素。

可以使用此成员函数替代 begin() 成员函数,以保证返回值为 const_iterator。 通常情况下,结合使用自动类型推导关键字,如下面的示例中所示。 在示例中,请考虑Container的可修改 (非const) 容器支持任何种类的begin()cbegin()

C++
 
auto i1 = Container.begin();
// i1 is Container<T>::iterator   
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator  

返回一个 const 迭代器,此迭代器用于发现刚超出范围中最后一个元素的位置。

const_iterator cend() const;

返回值

指向刚超出范围末尾的位置的 const 双向访问迭代器。

备注

cend 用于测试迭代器是否超过了其范围的末尾。

可以使用此成员函数替代 end() 成员函数,以保证返回值为 const_iterator。 通常情况下,结合使用自动类型推导关键字,如下面的示例中所示。 在示例中,请考虑Container的可修改 (非const) 容器支持任何种类的end()cend()

C++
 
auto i1 = Container.end();
// i1 is Container<T>::iterator   
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator  

不应对 cend 返回的值取消引用。

消除列表中的全部元素。

void clear();

示例

// list_clear.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main() {  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
  
   cout << "The size of the list is initially " << c1.size( ) << endl;  
   c1.clear( );  
   cout << "The size of list after clearing is " << c1.size( ) << endl;  
}  

The size of the list is initially 3  
The size of list after clearing is 0  

一种提供双向迭代器可读取const在列表中的元素。

typedef implementation-defined const_iterator;  

备注

const_iterator 类型不能用于修改元素的值。

示例

请参阅示例

提供指向的const在列表中的元素。

typedef typename Allocator::const_pointer const_pointer;  

备注

const_pointer 类型不能用于修改元素的值。

在大多数情况下,迭代器应可用于访问列表对象中的元素。

提供对引用的类型const存储于列表中供读取和执行元素const操作。

typedef typename Allocator::const_reference const_reference;  

备注

const_reference 类型不能用于修改元素的值。

示例

// list_const_ref.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
  
   const list <int> c2 = c1;  
   const int &i = c2.front( );  
   const int &j = c2.back( );  
   cout << "The first element is " << i << endl;  
   cout << "The second element is " << j << endl;  
  
   // The following line would cause an error because c2 is const  
   // c2.push_back( 30 );  
}  

The first element is 10  
The second element is 20  

一种提供双向迭代器可读取任何const在列表中的元素。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;  

备注

const_reverse_iterator 类型无法修改元素的值,它用于反向循环访问列表。

示例

请参阅示例rbegin

返回发现反向列表中第一个元素的位置的常量迭代器。

const_reverse_iterator rbegin() const;

返回值

一个常量反向双向迭代器定址发现反向列表中的第一个元素 (或发现曾是非反向的最后一个元素list)。

备注

crbegin用于发现反向列表就像list:: begin与使用list

返回值为 crbegin 时,无法修改列表对象。 list:: rbegin可用来向后循环访问列表。

示例

// list_crbegin.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::const_reverse_iterator c1_crIter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
   c1_crIter = c1.crbegin( );  
   cout << "The last element in the list is " << *c1_crIter << "." << endl;  
}  

The last element in the list is 30.  

返回用于发现反向列表中最后一个元素之后的位置的常量迭代器。

const_reverse_iterator rend() const;

返回值

一个常量反向双向迭代器,此迭代中发现反向最后一个元素之后的位置列表(在非反向之前的第一个元素的位置list)。

备注

crend用于发现反向列表就像list:: end与使用list

返回值为 crend 时,无法修改 list 对象。

crend 可用于测试反向迭代器是否已到达其 list 的末尾。

不应对 crend 返回的值取消引用。

示例

// list_crend.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::const_reverse_iterator c1_crIter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
  
   c1_crIter = c1.crend( );  
   c1_crIter --;  // Decrementing a reverse iterator moves it forward in   
                 // the list (to point to the first element here)  
   cout << "The first element in the list is: " << *c1_crIter << endl;  
}  

The first element in the list is: 10  

可用于表示列表中迭代器所指向元素之间元素数目的有符号整数类型。

typedef typename Allocator::difference_type difference_type;  

备注

difference_type 是通过容器迭代器减少或递增时返回的类型。 difference_type通常用于表示在范围内的元素的数目 [ first, last) 迭代器之间firstlast,包括指向的元素first和元素,但不是包括的范围,该元素指向last

请注意,虽然difference_type可满足要求的输入迭代器,其中包括像集,迭代器之间的减法仅受随机访问迭代器提供的随机访问容器,例如可逆容器支持的双向迭代器的类的所有迭代器用于vector 类

示例

// list_diff_type.cpp  
// compile with: /EHsc  
#include <iostream>  
#include <list>  
#include <algorithm>  
  
int main( )   
{  
   using namespace std;  
  
   list <int> c1;  
   list <int>::iterator   c1_Iter, c2_Iter;  
  
   c1.push_back( 30 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
   c1.push_back( 10 );  
   c1.push_back( 30 );  
   c1.push_back( 20 );  
  
   c1_Iter = c1.begin( );  
   c2_Iter = c1.end( );  
  
    list <int>::difference_type df_typ1, df_typ2, df_typ3;  
  
   df_typ1 = count( c1_Iter, c2_Iter, 10 );  
   df_typ2 = count( c1_Iter, c2_Iter, 20 );  
   df_typ3 = count( c1_Iter, c2_Iter, 30 );  
   cout << "The number '10' is in c1 collection " << df_typ1 << " times.\n";  
   cout << "The number '20' is in c1 collection " << df_typ2 << " times.\n";  
   cout << "The number '30' is in c1 collection " << df_typ3 << " times.\n";  
}  

The number '10' is in c1 collection 1 times.  
The number '20' is in c1 collection 2 times.  
The number '30' is in c1 collection 3 times.  

将构造的元素插入到列表中的指定位置。

void emplace_back(iterator _Where, Type&& val);

参数

   
参数 描述
_Where 在目标中的位置列表插入的第一个元素的位置。
val 添加到 list 末尾的元素。

备注

如果引发了异常,list 将保持不变,该异常将被重新引发。

示例

// list_emplace.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
#include <string>  
  
int main( )   
{  
   using namespace std;  
   list <string> c2;  
   string str("a");  
  
   c2.emplace(c2.begin(), move( str ) );  
   cout << "Moved first element: " << c2.back( ) << endl;  
}  

Moved first element: a  

在列表的起始位置添加一个就地构造的元素。

void emplace_back(Type&& val);

参数

   
参数 说明
val 添加到末尾的元素列表

备注

如果引发了异常,list 将保持不变,该异常将被重新引发。

示例

// list_emplace_back.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
#include <string>  
  
int main( )   
{  
   using namespace std;  
   list <string> c2;  
   string str("a");  
  
   c2.emplace_back( move( str ) );  
   cout << "Moved first element: " << c2.back( ) << endl;  
}  

Moved first element: a  

在列表的起始位置添加一个就地构造的元素。

void emplace_front(Type&& val);

参数

   
参数 说明
val 添加到开始处的元素列表

备注

如果引发了异常,list 将保持不变,该异常将被重新引发。

示例

// list_emplace_front.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
#include <string>  
  
int main( )   
{  
   using namespace std;  
   list <string> c2;  
   string str("a");  
  
   c2.emplace_front( move( str ) );  
   cout << "Moved first element: " << c2.front( ) << endl;  
}  

Moved first element: a  

测试列表是否为空。

bool empty() const;

返回值

true如果列表为空,则为false如果列表不为空。

示例

// list_empty.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
   if ( c1.empty( ) )  
      cout << "The list is empty." << endl;  
   else  
      cout << "The list is not empty." << endl;  
}  

The list is not empty.  

返回用于发现列表中最后一个元素之后的位置的迭代器。

const_iterator end() const;

iterator end();

返回值

用于发现列表中最后一个元素之后的位置的双向迭代器。 如果列表为空,则 list::end == list::begin

备注

结束用于测试是否迭代器已到达列表的末尾。

示例

// list_end.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
  
   c1_Iter = c1.end( );  
   c1_Iter--;  
   cout << "The last integer of c1 is " << *c1_Iter << endl;  
  
   c1_Iter--;  
 *c1_Iter = 400;  
   cout << "The new next-to-last integer of c1 is "  
        << *c1_Iter << endl;  
  
   // If a const iterator had been declared instead with the line:  
   // list <int>::const_iterator c1_Iter;  
   // an error would have resulted when inserting the 400  
  
   cout << "The list is now:";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
}  

The last integer of c1 is 30  
The new next-to-last integer of c1 is 400  
The list is now: 10 400 30  

从列表中的指定位置移除一个或一系列元素。

iterator erase(iterator _Where);

iterator erase(iterator first, iterator last);

参数

_Where
要从列表中移除的元素的位置。

first
要从列表中移除的第一个元素的位置。

last
要从列表中移除的刚超出最后一个元素的位置。

返回值

指定已移除的任何元素之外保留的第一个元素的双向迭代器;如果不存在此类元素,则为指向列表末尾的指针。

备注

不发生重新分配,因此迭代器和引用仅对已清除元素无效。

擦除永远不会引发异常。

示例

// list_erase.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator Iter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
   c1.push_back( 40 );  
   c1.push_back( 50 );  
   cout << "The initial list is:";  
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )  
      cout << " " << *Iter;  
   cout << endl;  
  
   c1.erase( c1.begin( ) );  
   cout << "After erasing the first element, the list becomes:";  
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )  
      cout << " " << *Iter;  
   cout << endl;  
   Iter = c1.begin( );  
   Iter++;  
   c1.erase( Iter, c1.end( ) );  
   cout << "After erasing all elements but the first, the list becomes: ";  
   for (Iter = c1.begin( ); Iter != c1.end( ); Iter++ )  
      cout << " " << *Iter;  
   cout << endl;  
}  

The initial list is: 10 20 30 40 50  
After erasing the first element, the list becomes: 20 30 40 50  
After erasing all elements but the first, the list becomes:  20  

返回对列表中第一个元素的引用。

reference front();

const_reference front() const;

返回值

如果列表为空,返回的结果则不确定。

备注

如果将 front 的返回值分配给 const_reference,则无法修改列表对象。 如果返回值为front分配给引用,可以修改列表对象。

通过使用在编译时_ITERATOR_DEBUG_LEVEL定义为 1 或 2,运行时将发生错误如果试图访问空列表中的元素。 有关更多信息,请参见 Checked Iterators 。

示例

// list_front.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main() {  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
  
   int& i = c1.front();  
   const int& ii = c1.front();  
  
   cout << "The first integer of c1 is " << i << endl;  
   i++;  
   cout << "The first integer of c1 is " << ii << endl;  
}  

The first integer of c1 is 10  
The first integer of c1 is 11  

返回用于构造列表的分配器对象的一个副本。

Allocator get_allocator() const;

返回值

列表使用的分配器。

备注

列表类的分配器指定类管理存储的方式。 STL 容器类提供的默认分配器足以满足大多编程需求。 编写和使用你自己的分配器类是高级 C++ 主题。

示例

// list_get_allocator.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   // The following lines declare objects   
   // that use the default allocator.  
   list <int> c1;  
   list <int, allocator<int> > c2 = list <int, allocator<int> >( allocator<int>( ) );  
  
   // c3 will use the same allocator class as c1  
   list <int> c3( c1.get_allocator( ) );  
  
   list<int>::allocator_type xlst = c1.get_allocator( );  
   // You can now call functions on the allocator class used by c1  
}  

将一个、几个或一系列元素插入列表中的指定位置。

iterator insert(
    iterator Where,  
    const Type& Val);

iterator insert(
    iterator Where,  
    Type&& Val);

void insert(
    iterator Where,  
    size_type Count,  
    const Type& Val);

iterator insert(
    iterator Where,  
    initializer_list<Type> IList);

template <class InputIterator>  
void insert(
    iterator Where,  
    InputIterator First,  
    InputIterator Last);

参数

   
参数 描述
Where 目标列表中插入第一个元素的位置。
Val 要插入到列表中的元素的值。
Count 要插入列表中的元素数目。
First 要从参数列表中复制的一系列元素中第一个元素的位置。
Last 要从自变量列表中复制的一系列元素以外的第一个元素的位置。

返回值

前两个插入函数返回一个迭代器,该迭代器指向新元素插入到列表中的位置。

示例

// list_class_insert.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
#include <string>  
  
int main()  
{  
    using namespace std;  
    list <int> c1, c2;  
    list <int>::iterator Iter;  
  
    c1.push_back(10);  
    c1.push_back(20);  
    c1.push_back(30);  
    c2.push_back(40);  
    c2.push_back(50);  
    c2.push_back(60);  
  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    Iter = c1.begin();  
    Iter++;  
    c1.insert(Iter, 100);  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    Iter = c1.begin();  
    Iter++;  
    Iter++;  
    c1.insert(Iter, 2, 200);  
  
    cout << "c1 =";  
    for(auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    c1.insert(++c1.begin(), c2.begin(), --c2.end());  
  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    // initialize a list of strings by moving  
    list < string > c3;  
    string str("a");  
  
    c3.insert(c3.begin(), move(str));  
    cout << "Moved first element: " << c3.front() << endl;  
  
    // Assign with an initializer_list  
    list <int> c4{ {1, 2, 3, 4} };  
    c4.insert(c4.begin(), { 5, 6, 7, 8 });  
  
    cout << "c4 =";  
    for (auto c : c4)  
        cout << " " << c;  
    cout << endl;  
}  

提供可读取或修改列表中任何元素的双向迭代器的类型。

typedef implementation-defined iterator;  

备注

一种类型迭代器可以用于修改元素的值。

示例

请参阅示例开始

构造一个列表,它具有特定大小或它的元素具有特定值,或具有特定分配器或作为其他列表的全部或部分副本。

list();

explicit list(
    const Allocator& Al);

explicit list(
    size_type Count);

list(
    size_type Count,  
    const Type& Val);

list(
    size_type Count,  
    const Type& Val,  
    const Allocator& Al);

list(
    const list& Right);

list(
    list&& Right);

list(
    initializer_list<Type> IList,  
    const Allocator& Al);

template <class InputIterator>  
list(
 InputIterator First,  
    InputIterator Last);

template <class InputIterator>  
list(
 InputIterator First,  
    InputIterator Last,  
    const Allocator& Al);

参数

   
参数 描述
Al 要用于此对象的分配器类。
Count 所构造列表中元素的数目。
Val 列表中元素的值。
Right 所构造列表要作为其副本的列表。
First 要复制的范围元素中的第一个元素的位置。
Last 要复制的元素范围以外的第一个元素的位置。
IList 包含要复制的元素的 initializer_list。

备注

所有构造函数都会存储一个分配器对象 ( Al) 并初始化列表。

get_allocator返回用于构造列表的分配器对象的副本。

前两个构造函数指定一个空的初始列表,第二个指定的分配器类型 ( Al) 使用。

第三个构造函数指定的指定数量的重复 ( Count) 的元素的默认值为类类型

第四个和第五个构造函数指定的重复 ( Count) 值的元素Val

第六个构造函数指定列表 Right 的副本。

第七个构造函数移动列表 Right

第八个构造函数使用 initializer_list 指定元素。

接下来的两个构造函数复制列表的范围 [First, Last)

所有构造函数均不执行任何临时重新分配。

示例

C++
// list_class_list.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    // Create an empty list c0  
    list <int> c0;  
  
    // Create a list c1 with 3 elements of default value 0  
    list <int> c1(3);  
  
    // Create a list c2 with 5 elements of value 2  
    list <int> c2(5, 2);  
  
    // Create a list c3 with 3 elements of value 1 and with the   
    // allocator of list c2  
    list <int> c3(3, 1, c2.get_allocator());  
  
    // Create a copy, list c4, of list c2  
    list <int> c4(c2);  
  
    // Create a list c5 by copying the range c4[ first,  last)  
    list <int>::iterator c4_Iter = c4.begin();  
    c4_Iter++;  
    c4_Iter++;  
    list <int> c5(c4.begin(), c4_Iter);  
  
    // Create a list c6 by copying the range c4[ first,  last) and with   
    // the allocator of list c2  
    c4_Iter = c4.begin();  
    c4_Iter++;  
    c4_Iter++;  
    c4_Iter++;  
    list <int> c6(c4.begin(), c4_Iter, c2.get_allocator());  
  
    cout << "c1 =";  
    for (auto c : c1)  
        cout << " " << c;  
    cout << endl;  
  
    cout << "c2 =";  
    for (auto c : c2)  
        cout << " " << c;  
    cout << endl;  
  
    cout << "c3 =";  
    for (auto c : c3)  
        cout << " " << c;  
    cout << endl;  
  
    cout << "c4 =";  
    for (auto c : c4)  
        cout << " " << c;  
    cout << endl;  
  
    cout << "c5 =";  
    for (auto c : c5)  
        cout << " " << c;  
    cout << endl;  
  
    cout << "c6 =";  
    for (auto c : c6)  
        cout << " " << c;  
    cout << endl;  
  
    // Move list c6 to list c7  
    list <int> c7(move(c6));  
    cout << "c7 =";  
    for (auto c : c7)  
        cout << " " << c;  
    cout << endl;  
  
    // Construct with initializer_list  
    list<int> c8({ 1, 2, 3, 4 });  
    cout << "c8 =";  
    for (auto c : c8)  
        cout << " " << c;  
    cout << endl;  
}  

c1 = 0 0 0c2 = 2 2 2 2 2c3 = 1 1 1c4 = 2 2 2 2 2c5 = 2 2c6 = 2 2 2c7 = 2 2 2c8 = 1 2 3 4  

返回列表的最大长度。

size_type max_size() const;

返回值

列表的最大可取长度。

示例

// list_max_size.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::size_type i;  
  
   i = c1.max_size( );  
   cout << "Maximum possible length of the list is " << i << "." << endl;  
}  

将元素从参数列表移除,将它们插入目标列表,将新的组合元素集以升序或其他指定顺序排序。

void merge(
    list<Type, Allocator>& right);

template <class Traits>  
void merge(
    list<Type, Allocator>& right,   
    Traits comp);

参数

right
要与目标列表合并的自变量列表。

comp
用于排列目标列表元素的比较运算符。

备注

参数列表 right 与目标列表合并。

参数列表和目标列表必须用相同的比较关系进行排序,生成的序列将以这种关系进行排序。 第一个成员函数的默认排列顺序是升序。 第二个成员函数执行用户指定的比较运算comp类的特征

示例

// list_merge.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1, c2, c3;  
   list <int>::iterator c1_Iter, c2_Iter, c3_Iter;  
  
   c1.push_back( 3 );  
   c1.push_back( 6 );  
   c2.push_back( 2 );  
   c2.push_back( 4 );  
   c3.push_back( 5 );  
   c3.push_back( 1 );  
  
   cout << "c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   cout << "c2 =";  
   for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )  
      cout << " " << *c2_Iter;  
   cout << endl;  
  
   c2.merge( c1 );  // Merge c1 into c2 in (default) ascending order  
   c2.sort( greater<int>( ) );  
   cout << "After merging c1 with c2 and sorting with >: c2 =";  
   for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )  
      cout << " " << *c2_Iter;  
   cout << endl;  
  
   cout << "c3 =";  
   for ( c3_Iter = c3.begin( ); c3_Iter != c3.end( ); c3_Iter++ )  
      cout << " " << *c3_Iter;  
   cout << endl;  
  
   c2.merge( c3, greater<int>( ) );  
   cout << "After merging c3 with c2 according to the '>' comparison relation: c2 =";  
   for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )  
      cout << " " << *c2_Iter;  
   cout << endl;  
}  

c1 = 3 6  
c2 = 2 4  
After merging c1 with c2 and sorting with >: c2 = 6 4 3 2  
c3 = 5 1  
After merging c3 with c2 according to the '>' comparison relation: c2 = 6 5 4 3 2 1  

用另一个列表的副本替换列表中的元素。

list& operator=(const list& right);

list& operator=(list&& right);

参数

   
参数 描述
right 列表复制到list

备注

消除 list 中的任何现有元素后,运算符会将 right 的内容复制或移动到 list 内。

示例

// list_operator_as.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   list<int> v1, v2, v3;  
   list<int>::iterator iter;  
  
   v1.push_back(10);  
   v1.push_back(20);  
   v1.push_back(30);  
   v1.push_back(40);  
   v1.push_back(50);  
  
   cout << "v1 = " ;  
   for (iter = v1.begin(); iter != v1.end(); iter++)  
      cout << *iter << " ";  
   cout << endl;  
  
   v2 = v1;  
   cout << "v2 = ";  
   for (iter = v2.begin(); iter != v2.end(); iter++)  
      cout << *iter << " ";  
   cout << endl;  
  
// move v1 into v2  
   v2.clear();  
   v2 = forward< list<int> >(v1);  
   cout << "v2 = ";  
   for (iter = v2.begin(); iter != v2.end(); iter++)  
      cout << *iter << " ";  
   cout << endl;  
}  

提供指向列表元素的指针。

typedef typename Allocator::pointer pointer;  

备注

一种类型指针可以用于修改元素的值。

在大多数情况下,迭代器应可用于访问列表对象中的元素。

删除列表末尾的元素。

void pop_back();

备注

最后一个元素不得为空。 pop_back 绝不会引发异常。

示例

// list_pop_back.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 1 );  
   c1.push_back( 2 );  
   cout << "The first element is: " << c1.front( ) << endl;  
   cout << "The last element is: " << c1.back( ) << endl;  
  
   c1.pop_back( );  
   cout << "After deleting the element at the end of the list, "  
           "the last element is: " << c1.back( ) << endl;  
}  

The first element is: 1  
The last element is: 2  
After deleting the element at the end of the list, the last element is: 1  

删除列表起始处的一个元素。

void pop_front();

备注

第一个元素不得为空。 pop_front 绝不会引发异常。

示例

// list_pop_front.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 1 );  
   c1.push_back( 2 );  
   cout << "The first element is: " << c1.front( ) << endl;  
   cout << "The second element is: " << c1.back( ) << endl;  
  
   c1.pop_front( );  
   cout << "After deleting the element at the beginning of the list, "  
         "the first element is: " << c1.front( ) << endl;  
}  

The first element is: 1  
The second element is: 2  
After deleting the element at the beginning of the list, the first element is: 2  

在列表的末尾添加元素。

void push_back(void push_back(Type&& val);

参数

   
参数 描述
val 添加到列表末尾的元素。

备注

如果引发异常,列表将保持不变,该异常将被重新引发。

示例

// list_push_back.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
#include <string>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 1 );  
   if ( c1.size( ) != 0 )  
      cout << "Last element: " << c1.back( ) << endl;  
  
   c1.push_back( 2 );  
   if ( c1.size( ) != 0 )  
      cout << "New last element: " << c1.back( ) << endl;  
  
// move initialize a list of strings  
   list <string> c2;  
   string str("a");  
  
   c2.push_back( move( str ) );  
   cout << "Moved first element: " << c2.back( ) << endl;  
}  

Last element: 1  
New last element: 2  
Moved first element: a  

在列表的开头添加元素。

void push_front(const Type& val);

void push_front(Type&& val);

参数

   
参数 描述
val 要添加到列表开头的元素。

备注

如果引发异常,列表将保持不变,该异常将被重新引发。

示例

// list_push_front.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
#include <string>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_front( 1 );  
   if ( c1.size( ) != 0 )  
      cout << "First element: " << c1.front( ) << endl;  
  
   c1.push_front( 2 );  
   if ( c1.size( ) != 0 )  
      cout << "New first element: " << c1.front( ) << endl;  
  
// move initialize a list of strings  
   list <string> c2;  
   string str("a");  
  
   c2.push_front( move( str ) );  
   cout << "Moved first element: " << c2.front( ) << endl;  
}  

First element: 1  
New first element: 2  
Moved first element: a  

返回一个迭代器,此迭代器用于发现反向列表中的第一个元素。

const_reverse_iterator rbegin() const;

reverse_iterator rbegin();

返回值

一个发现反向列表中的第一个元素(或发现非反向列表中的最后一个元素的内容)的反向双向迭代器。

备注

rbegin用于发现反向列表就像开始与列表一起使用。

如果将 rbegin 的返回值分配给 const_reverse_iterator,则无法修改列表对象。 如果将 rbegin 的返回值分配给 reverse_iterator,则可修改列表对象。

rbegin 可用于向后循环访问列表。

示例

// list_rbegin.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter;  
   list <int>::reverse_iterator c1_rIter;  
  
   // If the following line replaced the line above, *c1_rIter = 40;  
   // (below) would be an error  
   //list <int>::const_reverse_iterator c1_rIter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
   c1_rIter = c1.rbegin( );  
   cout << "The last element in the list is " << *c1_rIter << "." << endl;  
  
   cout << "The list is:";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   // rbegin can be used to start an iteration through a list in   
   // reverse order  
   cout << "The reversed list is:";  
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )  
      cout << " " << *c1_rIter;  
   cout << endl;  
  
   c1_rIter = c1.rbegin( );  
 *c1_rIter = 40;  
   cout << "The last element in the list is now " << *c1_rIter << "." << endl;  
}  

The last element in the list is 30.  
The list is: 10 20 30  
The reversed list is: 30 20 10  
The last element in the list is now 40.  

提供对存储在列表中的元素的引用的类型。

typedef typename Allocator::reference reference;  

示例

// list_ref.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
  
   int &i = c1.front( );  
   int &j = c1.back( );  
   cout << "The first element is " << i << endl;  
   cout << "The second element is " << j << endl;  
}  

The first element is 10  
The second element is 20  

清除列表中与指定值匹配的元素。

void remove(const Type& val);

参数

val
一个值,如果某个元素包含该值,则会导致从列表中删除该元素。

备注

剩余元素的排序不受影响。

示例

// list_remove.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter, c2_Iter;  
  
   c1.push_back( 5 );  
   c1.push_back( 100 );  
   c1.push_back( 5 );  
   c1.push_back( 200 );  
   c1.push_back( 5 );  
   c1.push_back( 300 );  
  
   cout << "The initial list is c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   list <int> c2 = c1;  
   c2.remove( 5 );  
   cout << "After removing elements with value 5, the list becomes c2 =";  
   for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )  
      cout << " " << *c2_Iter;  
   cout << endl;  
}  

The initial list is c1 = 5 100 5 200 5 300  
After removing elements with value 5, the list becomes c2 = 100 200 300  

清除列表将满足指定的谓词的元素。

template <class Predicate>  
void remove_if(Predicate pred)  

参数

pred
一元谓词,这样,如果某个元素不满足在删除该元素从列表中。

示例

// list_remove_if.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
template <class T> class is_odd : public std::unary_function<T, bool>   
{  
public:  
   bool operator( ) ( T& val )   
   {  
   return ( val % 2 ) == 1;  
   }  
};  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter, c2_Iter;  
  
   c1.push_back( 3 );  
   c1.push_back( 4 );  
   c1.push_back( 5 );  
   c1.push_back( 6 );  
   c1.push_back( 7 );  
   c1.push_back( 8 );  
  
   cout << "The initial list is c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   list <int> c2 = c1;  
   c2.remove_if( is_odd<int>( ) );  
  
   cout << "After removing the odd elements, "  
        << "the list becomes c2 =";  
   for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )  
      cout << " " << *c2_Iter;  
   cout << endl;  
}  

The initial list is c1 = 3 4 5 6 7 8  
After removing the odd elements, the list becomes c2 = 4 6 8  

返回用于发现反向列表中的最后元素之后的位置的迭代器。

const_reverse_iterator rend() const;

reverse_iterator rend();

返回值

用于发现反向列表 (非反向列表中之前的第一个元素的位置) 中的最后一个元素之后的位置的反向双向迭代器。

备注

rend用于发现反向列表就像结束与列表一起使用。

如果将 rend 的返回值分配给 const_reverse_iterator,则无法修改列表对象。 如果将 rend 的返回值分配给 reverse_iterator,则可修改列表对象。

rend可以用于测试反向迭代器是否已到达列表的末尾。

不应对 rend 返回的值取消引用。

示例

// list_rend.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter;  
   list <int>::reverse_iterator c1_rIter;  
  
   // If the following line had replaced the line above, an error would   
   // have resulted in the line modifying an element (commented below)  
   // because the iterator would have been const  
   // list <int>::const_reverse_iterator c1_rIter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
  
   c1_rIter = c1.rend( );  
   c1_rIter --;  // Decrementing a reverse iterator moves it forward in   
                 // the list (to point to the first element here)  
   cout << "The first element in the list is: " << *c1_rIter << endl;  
  
   cout << "The list is:";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   // rend can be used to test if an iteration is through all of the   
   // elements of a reversed list  
   cout << "The reversed list is:";  
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )  
      cout << " " << *c1_rIter;  
   cout << endl;  
  
   c1_rIter = c1.rend( );  
   c1_rIter--;  // Decrementing the reverse iterator moves it backward   
                // in the reversed list (to the last element here)  
  
 *c1_rIter = 40;  // This modification of the last element would have   
                    // caused an error if a const_reverse iterator had   
                    // been declared (as noted above)  
  
   cout << "The modified reversed list is:";  
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )  
      cout << " " << *c1_rIter;  
   cout << endl;  
}  

The first element in the list is: 10  
The list is: 10 20 30  
The reversed list is: 30 20 10  
The modified reversed list is: 30 20 40  

为列表指定新的大小。

void resize(size_type _Newsize);

void resize(size_type _Newsize, Type val);

参数

_Newsize
新列表的大小。

val
要添加到列表中,如果新大小大于新元素的值的原始大小。 如果省略该值,则新元素被指定类的默认值。

备注

如果此列表中的大小小于请求的大小, _Newsize,元素添加到列表中,直到它达到请求的大小。

如果此列表中的大小大于请求的大小,靠近列表末尾元素将被删除直到列表达到大小_Newsize

如果列表的当前大小与请求的大小相同,则不执行任何操作。

大小反映列表的当前大小。

示例

// list_resize.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{   
   using namespace std;  
   list <int> c1;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
  
   c1.resize( 4,40 );  
   cout << "The size of c1 is " << c1.size( ) << endl;  
   cout << "The value of the last element is " << c1.back( ) << endl;  
  
   c1.resize( 5 );  
   cout << "The size of c1 is now " << c1.size( ) << endl;  
   cout << "The value of the last element is now " << c1.back( ) << endl;  
  
   c1.resize( 2 );  
   cout << "The reduced size of c1 is: " << c1.size( ) << endl;  
   cout << "The value of the last element is now " << c1.back( ) << endl;  
}  

The size of c1 is 4  
The value of the last element is 40  
The size of c1 is now 5  
The value of the last element is now 0  
The reduced size of c1 is: 2  
The value of the last element is now 20  

反转列表中元素的顺序。

void reverse();

示例

// list_reverse.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter;  
  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 30 );  
  
   cout << "c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   c1.reverse( );  
   cout << "Reversed c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
}  

c1 = 10 20 30  
Reversed c1 = 30 20 10  

提供可读取或修改反向列表中的元素的双向迭代器的类型。

typedef std::reverse_iterator<iterator> reverse_iterator;  

备注

reverse_iterator 类型用于反向循环访问列表。

示例

请参阅示例rbegin

返回列表中元素的数目。

size_type size() const;

返回值

列表的当前长度。

示例

// list_size.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   list <int> c1;  
   list <int>::size_type i;  
  
   c1.push_back( 5 );  
   i = c1.size( );  
   cout << "List length is " << i << "." << endl;  
  
   c1.push_back( 7 );  
   i = c1.size( );  
   cout << "List length is now " << i << "." << endl;  
}  

List length is 1.  
List length is now 2.  

计算列表中元素的数目的类型。

typedef typename Allocator::size_type size_type;  

示例

请参阅示例大小

按升序或用户指定的其他顺序排列列表元素。

void sort();

template <class Traits>  
void sort(Traits comp);

参数

comp
用于排列连续元素的比较运算符。

备注

默认情况下,第一个成员函数将按升序排列元素。

成员模板函数根据用户指定的比较运算的元素进行排序comp类的特征

示例

// list_sort.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter;  
  
   c1.push_back( 20 );  
   c1.push_back( 10 );  
   c1.push_back( 30 );  
  
   cout << "Before sorting: c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   c1.sort( );  
   cout << "After sorting c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   c1.sort( greater<int>( ) );  
   cout << "After sorting with 'greater than' operation, c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
}  

Before sorting: c1 = 20 10 30  
After sorting c1 = 10 20 30  
After sorting with 'greater than' operation, c1 = 30 20 10  

从源列表中删除元素并将其插入到目标列表中。

// insert the entire source list  
void splice(const_iterator Where, list<Type, Allocator>& Source);

void splice(const_iterator Where, list<Type, Allocator>&& Source);

 
// insert one element of the source list  
void splice(const_iterator Where, list<Type, Allocator>& Source, const_iterator Iter);

void splice(const_iterator Where, list<Type, Allocator>&& Source, const_iterator Iter);

 
// insert a range of elements from the source list  
void splice(const_iterator Where, list<Type, Allocator>& Source, const_iterator First, const_iterator Last);

    void splice(const_iterator Where, list<Type, Allocator>&& Source, const_iterator First, const_iterator Last);

参数

Where
目标列表中要在其前面进行插入的位置。

Source
要插入目标列表中的源列表。

Iter
要从源列表中进行插入的元素。

First
要从源列表中进行插入的范围中的第一个元素。

Last
要从源列表中进行插入的范围中的最后一个元素之外的第一个位置。

备注

第一对成员函数在 Where 所引用的位置之前,将源列表中的所有元素插入到目标列表中,然后从源列表中删除所有元素。 (&Source不能等于this。)

第二对成员函数将在 Iter 所引用的目标列表中的位置之前,插入 Where 所引用的元素,然后从源列表中删除 Iter。 (如果 Where == Iter || Where == ++Iter,则不会发生更改。)

第三对成员函数插入由指定的范围 [ First, Last) 引用的目标列表中的元素之前Where并从源列表中移除该元素范围。 (如果 &Source == this,则范围 [First, Last) 不得包含 Where 所指向的元素。)

如果范围的接合插入N元素,并&Source != this,类的对象迭代器递增N时间。

在所有情况下,迭代器、指针或引用接合的元素的引用都会保持有效状态且会转换为目标容器。

示例

C++
// list_splice.cpp  
// compile with: /EHsc /W4  
#include <list>  
#include <iostream>  
  
using namespace std;  
  
template <typename S> void print(const S& s) {  
    cout << s.size() << " elements: ";  
  
    for (const auto& p : s) {  
        cout << "(" << p << ") ";  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    list<int> c1{10,11};  
    list<int> c2{20,21,22};  
    list<int> c3{30,31};  
    list<int> c4{40,41,42,43};  
  
    list<int>::iterator where_iter;  
    list<int>::iterator first_iter;  
    list<int>::iterator last_iter;  
  
    cout << "Beginning state of lists:" << endl;  
    cout << "c1 = ";  
    print(c1);  
    cout << "c2 = ";  
    print(c2);  
    cout << "c3 = ";  
    print(c3);  
    cout << "c4 = ";  
    print(c4);  
  
    where_iter = c2.begin();  
    ++where_iter; // start at second element  
    c2.splice(where_iter, c1);  
    cout << "After splicing c1 into c2:" << endl;  
    cout << "c1 = ";  
    print(c1);  
    cout << "c2 = ";  
    print(c2);  
  
    first_iter = c3.begin();  
    c2.splice(where_iter, c3, first_iter);  
    cout << "After splicing the first element of c3 into c2:" << endl;  
    cout << "c3 = ";  
    print(c3);  
    cout << "c2 = ";  
    print(c2);  
  
    first_iter = c4.begin();  
    last_iter = c4.end();  
    // set up to get the middle elements  
    ++first_iter;  
    --last_iter;  
    c2.splice(where_iter, c4, first_iter, last_iter);  
    cout << "After splicing a range of c4 into c2:" << endl;  
    cout << "c4 = ";  
    print(c4);  
    cout << "c2 = ";  
    print(c2);  
}  
  

Beginning state of lists:c1 = 2 elements: (10) (11)c2 = 3 elements: (20) (21) (22)c3 = 2 elements: (30) (31)c4 = 4 elements: (40) (41) (42) (43)After splicing c1 into c2:c1 = 0 elements:c2 = 5 elements: (20) (10) (11) (21) (22)After splicing the first element of c3 into c2:c3 = 1 elements: (31)c2 = 6 elements: (20) (10) (11) (30) (21) (22)After splicing a range of c4 into c2:c4 = 2 elements: (40) (43)c2 = 8 elements: (20) (10) (11) (30) (41) (42) (21) (22)  

交换两个列表的元素。

void swap(list<Type, Allocator>& right);

friend void swap(list<Type, Allocator>& left, list<Type, Allocator>& right)  

参数

right
提供要交换的元素的列表,或其元素将要与列表 left 的元素交换的列表。

left
其元素将与列表 right 的进行交换的列表。

示例

// list_swap.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   list <int> c1, c2, c3;  
   list <int>::iterator c1_Iter;  
  
   c1.push_back( 1 );  
   c1.push_back( 2 );  
   c1.push_back( 3 );  
   c2.push_back( 10 );  
   c2.push_back( 20 );  
   c3.push_back( 100 );  
  
   cout << "The original list c1 is:";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   c1.swap( c2 );  
  
   cout << "After swapping with c2, list c1 is:";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   swap( c1,c3 );  
  
   cout << "After swapping with c3, list c1 is:";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
}  

The original list c1 is: 1 2 3  
After swapping with c2, list c1 is: 10 20  
After swapping with c3, list c1 is: 100  

从列表中删除满足某些其他二元谓词的相邻重复元素或相邻元素。

void unique();

template <class BinaryPredicate>  
void unique(BinaryPredicate pred);

参数

pred
用于比较连续元素的二元谓词。

备注

此函数假设列表是经过排序的,因此所有重复元素都是相邻的。 不相邻的重复元素将不被删除。

第一个成员函数删除比较等于其前一个元素的每个元素。

第二个成员函数删除与前一个元素比较时满足谓词函数 pred 的元素。 您可以使用任何中声明的二元函数对象<functional>标头参数 pred 或者你可以创建您自己。

示例

// list_unique.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   list <int> c1;  
   list <int>::iterator c1_Iter, c2_Iter,c3_Iter;  
   not_equal_to<int> mypred;  
  
   c1.push_back( -10 );  
   c1.push_back( 10 );  
   c1.push_back( 10 );  
   c1.push_back( 20 );  
   c1.push_back( 20 );  
   c1.push_back( -10 );  
  
   cout << "The initial list is c1 =";  
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )  
      cout << " " << *c1_Iter;  
   cout << endl;  
  
   list <int> c2 = c1;  
   c2.unique( );  
   cout << "After removing successive duplicate elements, c2 =";  
   for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )  
      cout << " " << *c2_Iter;  
   cout << endl;  
  
   list <int> c3 = c2;  
   c3.unique( mypred );  
   cout << "After removing successive unequal elements, c3 =";  
   for ( c3_Iter = c3.begin( ); c3_Iter != c3.end( ); c3_Iter++ )  
      cout << " " << *c3_Iter;  
   cout << endl;  
}  

The initial list is c1 = -10 10 10 20 20 -10  
After removing successive duplicate elements, c2 = -10 10 20 -10  
After removing successive unequal elements, c3 = -10 -10  

表示列表中存储的数据类型的类型。

typedef typename Allocator::value_type value_type;  

备注

value_type是模板参数的同义词类型

示例

// list_value_type.cpp  
// compile with: /EHsc  
#include <list>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   list<int>::value_type AnInt;  
   AnInt = 44;  
   cout << AnInt << endl;  
}  

44  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值