📅  最后修改于: 2020-12-15 04:32:19             🧑  作者: Mango
List是一种常用的序列容器。容器是保存相同类型数据的对象。列表容器被实现为双链表,因此它提供了对其数据的双向顺序访问。
列表不提供快速随机访问,它仅支持双向顺序访问。 List允许在恒定时间内在序列中的任何位置进行插入和删除操作。
列表的元素可以分散在不同的内存块中。容器存储必要的信息,以允许顺序访问其数据。列表可以在运行时从两端根据需要缩小或扩展。内部分配器自动满足存储要求。
大小为零的列表也有效。在这种情况下,list.begin()和list.end()指向同一位置。但是调用front()或back()的行为是不确定的。
以下是头文件中std :: list的定义
template < class T, class Alloc = allocator > class list;
T-所包含元素的类型。
T可以用任何其他数据类型(包括用户定义的类型)代替。
Alloc-分配器对象的类型。
默认情况下,使用分配器类模板,该模板定义了最简单的内存分配模型并且与值无关。
成员函数可以将以下成员类型用作参数或返回类型。
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 |
10 | const_reverse_iterator | std::reverse_iterator |
11 | size_type | size_t |
12 | difference_type | ptrdiff_t |
以下是标头中所有方法的列表。
Sr.No. | Method & Description |
---|---|
1 | list::list default constructor Constructs an empty list with zero elements. |
2 | list::list fill constructor
Constructs a new list with n elements and assigns val to each element of list. |
3 | list::list fill constructor
Constructs a new list with n elements and assign zero value to each element of list. |
4 | list::list range constructor
Constructs a list with as many elements as in range of first to last. |
5 | list::list copy constructor
Constructs a list with copy of each elements present in existing list. |
6 | list::list move constructor
Constructs a list with the contents of other using move semantics. |
7 | list::list initializer list constructor
Constructs a list with the contents of other using move semantics. |
Sr.No. | Method & Description |
---|---|
1 | list::~list
Destroys list object by deallocating it’s memory. |
Sr.No. | Method & Description |
---|---|
1 | list::assign range version
Assigns new value to list by replacing old ones. |
2 | list::assign fill version
Assigns new values to list by replacing old ones. |
3 | list::assign initializer list version
Assigns new values to list by replacing old ones. |
4 | list::back
Returns a reference to the last element of the list. |
5 | list::begin
Returns a random access iterator which points to the first element of the list. |
6 | list::cbegin
Returns a constant random access iterator which points to the beginning of the list. |
7 | list::cend
Returns a constant random access iterator which points to the end of the list. |
8 | list::clear
Destroys the list by removing all elements from the list and sets size of list to zero. |
9 | list::crbegin
Returns a constant reverse iterator which points to the last element of the list. |
10 | list::crend
Returns a constant reverse iterator which points to the theoretical element preceding the first element in the list. |
11 | list::emplace
Extends list by inserting new element at a given position. |
12 | list::emplace_back
Inserts new element at the end of list and increases size of list by one. |
13 | list::emplace_front
Inserts new element at the beginning of the list and increases size of list by one. |
14 | list::empty
Tests whether list is empty or not. |
15 | list::end
Returns a random access iterator which points to the last element of the list. |
16 | list::erase position version
Removes single element from the the list. |
17 | list::erase range version
Removes range of element from the the list. |
18 | list::front
Returns a reference to the first element of the list. |
19 | list::get_allocator
Returns an allocator associated with list |
20 | list::insert single element version
Extends iterator by inserting new element at position in list. |
21 | list::insert fill version
Extends list by inserting new elements in the container. |
22 | list::insert range version
Extends list by inserting new elements in the container. |
23 | list::insert move version
Extends list by inserting new element in the container. |
24 | list::insert initializer list version
Extends list by inserting new elements in the container |
25 | list::max_size
Returns the maximum number of elements can be held by list. |
26 | list::merge
Merges two sorted lists into one. |
27 | list::merge compare function
Merges two sorted lists into one. |
28 | list::merge move version
Merges two sorted lists into one by using move semantics. |
29 | list::merge compare function move version
Merges 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_back
Removes last element from list. |
34 | list::pop_front
Removes first element from list. |
35 | list::push_back
Inserts new element at the end of list. |
36 | list::push_back move version
Inserts new element at the end of list. |
37 | list::push_front
Inserts new element at the beginning of list. |
38 | list::push_front move version
Inserts new element at the beginning of list. |
39 | list::rbegin
Returns a reverse iterator which points to the last element of the list. |
40 | list::remove
removes element(s) from the list that matches the value. |
41 | list::remove_if
removes elements from the list that fulfills the condition. |
42 | list::rend
Returns a reverse iterator which points to the reverse end of the list. |
43 | list::resize
Changes the size of list. |
44 | list::resize value version
Changes the size of list. |
45 | list::reverse
Reverses the order of the elements present in the list. |
46 | list::size
Returns the number of elements present in the list. |
47 | list::sort
Sorts the elements of the list. |
48 | list::sort compare function
Sorts the elements of the list. |
49 | list::splice
Transfers all elements from list to *this. |
50 | list::splice single element
Transfers 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::swap
Exchanges the content of list with contents of another list x. |
56 | list::unique
Removes all consecutive duplicate elements from the list. |
57 | list::unique
Removes all consecutive duplicate elements from the list. |
Sr.No. | Method & Description |
---|---|
1 | operator==
Tests 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 | swap
Exchanges the contents of two list. |