注:std::list C++11标准
list概述
template <class T, class Alloc = allocator<T> > class list;
list是一种序列容器,它允许在序列中的任意位置进行常数时间的插入和删除操作,并可以在两个方向上进行迭代(遍历)。
list容器是基于双链表实现的,可以将其包含的每个元素存储在不同且不相关的存储位置上。通过链接到前一个元素和后一个元素的每个元素的关联关系在链表内部保持顺序。
list与forward_list非常相似:主要的区别是forward_list对象是单向链表,因此只能单向(forward)迭代(遍历),占用空间更小也更高效。
与其他基本的标准序列容器(array、vector和deque)相比,list在任何位置进行插入、获取和移动元素等操作方面都表现得更好,因此在使用这些操作的算法中也表现得更好,比如排序算法。
与其他序列容器相比,list和forward_list的主要缺点是它们无法使用元素位置对元素直接访问。例如,要访问list中的第6个元素,必须从已知位置(如开始或结束)遍历到该位置,需要花费的时间与这些位置之间的距离呈线性关系。它们还要消耗一些额外的内存来保存将每个元素关联起来的链接信息(也就是指针)。
容器属性
- 顺序存储
顺序容器中的元素按照严格的线性顺序存储。各个元素通过使用它们在这个序列中的位置来访问。
- 双向链表
每个元素都保存了如何定位下一个和前一个元素的信息,允许在特定元素之前或之后(甚至在整个范围内)进行常数时间的插入和删除操作,但不允许直接随机访问。
- 分配器
容器使用allocator对象来动态处理其存储需求。
模板参数
- T
元素的类型。
别名为成员类型 list :: value_type。
- Alloc
用于定义分配模型的分配器对象的类型。默认情况下,使用allocator类模板,该模板定义最简单的内存分配模型,并且与值无关。
别名为成员类型 list :: allocator_type。
成员类型
| member type | definition | notes |
|---|---|---|
| value_type | The first template parameter (T) | |
| allocator_type | The second template parameter (Alloc) | defaults to: allocator<value_type> |
| reference | value_type& | |
| const_reference | const value_type& | |
| pointer | allocator_traits<allocator_type>::pointer | for the default allocator: value_type* |
| const_pointer | allocator_traits<allocator_type>::const_pointer | for the default allocator: const value_type* |
| iterator | a bidirectional iterator to value_type | convertible to const_iterator |
| const_iterator | a bidirectional iterator to const value_type | |
| reverse_iterator | reverse_iterator<iterator> | |
| const_reverse_iterator | reverse_iterator<const_iterator> | |
| difference_type | a signed integral type, identical to: iterator_traits<iterator>::difference_type | usually the same as ptrdiff_t |
| size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
成员函数
- (constructor) 构造函数
| default (1) |
explicit list (const allocator_type& alloc = allocator_type()); |
|---|---|
| fill (2) |
explicit list (size_type n); |
| range (3) |
template <class InputIterator>
list (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
|
| copy (4) |
list (const list& x); list (const list& x, const allocator_type& alloc); |
| move (5) |
list (list&& x); list (list&& x, const allocator_type& alloc); |
| initializer list (6) |
list (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
|
构造函数示例:
// constructing lists #include <iostream> #include <list> int main () { // constructors used in the same order as described above: std::list<int> first; // empty list of ints std::list<int> second (4,100); // four ints with value 100 std::list<int> third (second.begin(),second.end()); // iterating through second std::list<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are: "; for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++) std::cout << *it << ' '; std::cout << '\n'; return 0; } Output: The contents of fifth are: 16 2 77 29