Cpp Standard Library 简明教程
C++ Library - <list>
Introduction
列表是一个普遍使用的顺序容器。容器是一种存储相同类型数据类型数据的对象。列表容器作为双向链表实现,因此它提供对其数据进行双向顺序访问。
List is a popularly used sequence container. Container is an object that holds data of same type. List container is implemented as doubly linked-list, hence it provides bidirectional sequential access to it’s data.
列表不提供快速的随机访问,它仅支持双向顺序访问。列表允许在序列中的任何位置进行插入和删除操作,时间复杂度为常数。
List doesn’t provide fast random access, it only supports sequential access in both directions. List allows insertion and deletion operation anywhere within a sequence in constant time.
列表的元素可以分散在内存的不同块中。容器存储必要的信息以允许对其数据进行顺序访问。列表可以在运行时根据需要从两端收缩或扩展。存储需求由内部分配器自动满足。
Elements of list can be scattered in different chunks of memory. Container stores necessary information to allow sequential access to it’s data. Lists can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator.
零大小的列表也是有效的。在这种情况下,list.begin() 和 list.end() 指向相同位置。但是调用 front() 或 back() 的行为是未定义的。
Zero sized lists are also valid. In that case list.begin() and list.end() points to same location. But behavior of calling front() or back() is undefined.
Definition
下面是 <list> 头文件中 std::list 的定义
Below is definition of std::list from <list> header file
template < class T, class Alloc = allocator<T> > class list;
Parameters
-
T − Type of the element contained. T may be substituted by any other data type including user-defined type.
-
Alloc − Type of allocator object. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Member types
以下成员类型可作为参数或返回类型,由成员函数使用。
Following member types can be used as parameters or return type by member functions.
Sr.No. |
Member types |
Definition |
1 |
value_type |
T (First parameter of the template) |
2 |
allocator_type |
Alloc (Second parameter of the template) |
3 |
reference |
value_type& |
4 |
const_reference |
const value_type& |
5 |
pointer |
value_type* |
6 |
const_pointer |
const value_type* |
7 |
iterator |
a random access iterator to value_type |
8 |
const_iterator |
a random access iterator to const value_type |
9 |
reverse_iterator |
std::reverse_iterator <iterator> |
10 |
const_reverse_iterator |
std::reverse_iterator <const_iterator> |
11 |
size_type |
size_t |
12 |
difference_type |
ptrdiff_t |
Functions from <list>
以下是 <list> 头文件中的所有方法的列表。
Below is list of all methods from <list> header.
Constructors
Sr.No. |
Method & Description |
1 |
list::list default constructorConstructs an empty list with zero elements. |
2 |
list::list fill constructorConstructs a new list with n elements and assigns val to each element of list. |
3 |
list::list fill constructorConstructs a new list with n elements and assign zero value to each element of list. |
4 |
list::list range constructorConstructs a list with as many elements as in range of first to last. |
5 |
list::list copy constructorConstructs a list with copy of each elements present in existing list. |
6 |
list::list move constructorConstructs a list with the contents of other using move semantics. |
7 |
list::list initializer list constructorConstructs a list with the contents of other using move semantics. |
Destructor
Sr.No. |
Method & Description |
1 |
list::~listDestroys list object by deallocating it’s memory. |
Member functions
Sr.No. |
Method & Description |
1 |
list::assign range versionAssigns new value to list by replacing old ones. |
2 |
list::assign fill versionAssigns new values to list by replacing old ones. |
3 |
list::assign initializer list versionAssigns new values to list by replacing old ones. |
4 |
list::backReturns a reference to the last element of the list. |
5 |
list::beginReturns a random access iterator which points to the first element of the list. |
6 |
list::cbeginReturns a constant random access iterator which points to the beginning of the list. |
7 |
list::cendReturns a constant random access iterator which points to the end of the list. |
8 |
list::clearDestroys the list by removing all elements from the list and sets size of list to zero. |
9 |
list::crbeginReturns a constant reverse iterator which points to the last element of the list. |
10 |
list::crendReturns a constant reverse iterator which points to the theoretical element preceding the first element in the list. |
11 |
list::emplaceExtends list by inserting new element at a given position. |
12 |
list::emplace_backInserts new element at the end of list and increases size of list by one. |
13 |
list::emplace_frontInserts new element at the beginning of the list and increases size of list by one. |
14 |
list::emptyTests whether list is empty or not. |
15 |
list::endReturns a random access iterator which points to the last element of the list. |
16 |
list::erase position versionRemoves single element from the the list. |
17 |
list::erase range versionRemoves range of element from the the list. |
18 |
list::frontReturns a reference to the first element of the list. |
19 |
list::get_allocatorReturns an allocator associated with list |
20 |
list::insert single element versionExtends iterator by inserting new element at position in list. |
21 |
list::insert fill versionExtends list by inserting new elements in the container. |
22 |
list::insert range versionExtends list by inserting new elements in the container. |
23 |
list::insert move versionExtends list by inserting new element in the container. |
24 |
list::insert initializer list versionExtends list by inserting new elements in the container |
25 |
list::max_sizeReturns the maximum number of elements can be held by list. |
26 |
list::mergeMerges two sorted lists into one. |
27 |
list::merge compare functionMerges two sorted lists into one. |
28 |
list::merge move versionMerges two sorted lists into one by using move semantics. |
29 |
list::merge compare function move versionMerges two sorted lists into one by using move semantics. |
30 |
list::operator= copy version Assigns new contents to the list by replacing old ones. |
31 |
list::operator= move version Assign new contents to the list by replacing old ones. |
32 |
list::operator= initializer list version Assign new contents to the list by replacing old ones. |
33 |
list::pop_backRemoves last element from list. |
34 |
list::pop_frontRemoves first element from list. |
35 |
list::push_backInserts new element at the end of list. |
36 |
list::push_back move versionInserts new element at the end of list. |
37 |
list::push_frontInserts new element at the beginning of list. |
38 |
list::push_front move versionInserts new element at the beginning of list. |
39 |
list::rbeginReturns a reverse iterator which points to the last element of the list. |
40 |
list::removeremoves element(s) from the list that matches the value. |
41 |
list::remove_ifremoves elements from the list that fulfills the condition. |
42 |
list::rendReturns a reverse iterator which points to the reverse end of the list. |
43 |
list::resizeChanges the size of list. |
44 |
list::resize value versionChanges the size of list. |
45 |
list::reverseReverses the order of the elements present in the list. |
46 |
list::sizeReturns the number of elements present in the list. |
47 |
list::sortSorts the elements of the list. |
48 |
list::sort compare functionSorts the elements of the list. |
49 |
list::spliceTransfers all elements from list to *this. |
50 |
list::splice single elementTransfers a element pointed to by iterator i from list x into *this. |
51 |
list::splice move version Transfers all elements from list x to *this by using move semantics. |
52 |
list::splice range version Transfers the elements in the range of first to last from x to *this. |
53 |
list::splice single element move version Transfers the element pointed to by iterator i from list x into *this by using move semantics. |
54 |
list::splice range and move version Transfers the elements in the range of first to last from x to *this by using move semantics. |
55 |
list::swapExchanges the content of list with contents of another list x. |
56 |
list::uniqueRemoves all consecutive duplicate elements from the list. |
57 |
list::uniqueRemoves all consecutive duplicate elements from the list. |
Non-member overloaded functions
Sr.No. |
Method & Description |
1 |
[role="bare"]../cpp_standard_library/cpp_list_operator_equal_to.htmlTests whether two lists are equal or not. |
2 |
operator!=Tests whether two lists are equal or not. |
3 |
operator<Tests whether first list is less than other or not. |
4 |
operator⇐Tests whether first list is less than or equal to other or not. |
5 |
operator>Tests whether first list is greater than other or not. |
6 |
operator>=Tests whether first list is greater than or equal to other or not. |
7 |
swapExchanges the contents of two list. |