list也是一个顺序容器。元素在容器线性排列。list中元素间的存储位置无关联,其元素的先后关系由链表指针表示。
list容器有如下特点:
- 在容器的任意位置高效的插入或删除元素(常量时间复杂度),而不仅仅是在头尾;
- 可以在容器内或容器间高效的移动元素(常量时间复杂度);
- 可以正向或反向遍历数组。
相较于其它两种顺序容器(vector和deque),list在插入删除移动元素时的效率较高,一般用于排序算法。但是list容器,不能直接访问某元素。比如我们要访问第6个元素,我们只能从第一个或最后一个元素,一个一个的遍历过去,不像其它两种顺序容器,可以使用下标直接访问。另外由于需要存储额外的指针,list的空间开销也较其它两种容器大,尤其是在存储大量小元素时。
此容器模版声明如下:
template < class T, class Allocator = allocator<T> > class list;
1、构造函数
此模板容器提供如下构造函数:
explicit list ( const Allocator& = Allocator() );
explicit list ( size_type n, const T& value = T(), const Allocator& = Allocator() );
template < class InputIterator >
list ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
list ( const list<T,Allocator>& x );
2、迭代器函数
list容器提供begin()、end()、rbegin()、rend()四个迭起函数。基本用法同其它两种顺序容器。
3、容量函数
list容器提供以下容量函数:
empty,size,max_size,resize
4、元素访问
front
返回容器中的第一个元素。调用此函数,容器不能为空。
back
返回容器中的最后一个元素。调用此函数,容器不能为空。
5、修改函数
-
assign
- 为list容器赋新值,旧值将被释放。
-
push_front
- 在链表开始插入元素。
-
pop_front
- 删除链表开始的第一个元素。
-
push_back
- 在链表尾添加元素。
-
pop_back
- 删除链表尾最后一个元素。
-
insert
- 在任意位置插入元素。
-
erase
-
iterator erase ( iterator position ); iterator erase ( iterator first, iterator last );
- 删除由迭代器参数指定的一个或多个元素 [first, last) 。
-
swap
- 交换两个链表的元素。
-
clear
- 清空链表。
6、操作函数
-
splice
-
void splice ( iterator position, list<T,Allocator>& x ); void splice ( iterator position, list<T,Allocator>& x, iterator i ); void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last );
-
将链表
x 中的1个或多个元素移动到调用此函数的链表的
position位置。这个函数不调用被移动元素的析构函数和构造函数,有常量时间复杂度。在元素被移动后,指向被移动元素的迭代器将失效。
-
remove
- 删除指定值的元素。
-
remove_if
-
template <class Predicate> void remove_if ( Predicate pred );
- 删除所有 Predicate pred 判断返回 true的元素。这将调用被删除元素的析构函数,容器的size值减少删除的元素数。
-
参数 pred 可以是任何接受一个list元素类型的参数并返回bool值的表达式。可以是函数指针或实现了operator()操作符的类对象。
代码示例:
// list::remove_if #include <iostream> #include <list> using namespace std; // a predicate implemented as a function: bool single_digit (const int& value) { return (value<10); } // a predicate implemented as a class: class is_odd { public: bool operator() (const int& value) {return (value%2)==1; } }; int main () { int myints[]= {15,36,7,17,20,39,4,1}; list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1 mylist.remove_if (single_digit); // 15 36 17 20 39 mylist.remove_if (is_odd()); // 36 20 cout << "mylist contains:"; for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; return 0; }
-
unique
-
void unique ( ); template <class BinaryPredicate> void unique ( BinaryPredicate binary_pred );
- 不带参数的版本删除重复值的元素。
带参数的版本,将提供一个比较器BianryPredicate,用法remove_if中的比较器类似。
代码示例;
// list::unique #include <iostream> #include <cmath> #include <list> using namespace std; // a binary predicate implemented as a function: bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); } // a binary predicate implemented as a class: class is_near { public: bool operator() (double first, double second) { return (fabs(first-second)<5.0); } }; int main () { double mydoubles[]={ 12.15, 2.72, 73.0, 12.77, 3.14, 12.77, 73.35, 72.25, 15.3, 72.25 }; list<double> mylist (mydoubles,mydoubles+10); mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77, // 15.3, 72.25, 72.25, 73.0, 73.35 mylist.unique(); // 2.72, 3.14, 12.15, 12.77 // 15.3, 72.25, 73.0, 73.35 mylist.unique (same_integral_part); // 2.72, 3.14, 12.15 // 15.3, 72.25, 73.0 mylist.unique (is_near()); // 2.72, 12.15, 72.25 cout << "mylist contains:"; for (list<double>::iterator it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; return 0; }
-
merge
- 合并排好序的链表。如果链表没有排好序,调用此函数会怎样呢?调用不会报错,但合并的链表并不是排好序的。
示例代码:
#include <list> #include <iostream> using namespace std; int main () { int ar1[] = {1, 5, 3, 9, 7}; int ar2[] = {2, 4, 8, 6, 0}; list<int> a1 (ar1, ar1 + sizeof(ar1)/sizeof(int)); list<int> a2 (ar2, ar2 + sizeof(ar2)/sizeof(int)); a1.sort (); a2.sort (); a1.merge (a2); list<int> &first = a1; cout << "first contains:"; for (list<int>::iterator it=first.begin(); it!=first.end(); ++it) cout << " " << *it; cout << endl; }
-
sort
- 排序
-
reverse
- 反向