list<>使用双向链表管理元素, 使用list需包含#include <list>
list的内部结构完全迥异于array,vector,deque。list对象提供了两个指针, 用来指向第一个和最后一个元素, 而且每个元素又有指针指向前一个和后一个元素。下图是追加一个元素后, list内部结构的变化
list与array,vector,deque主要有几点不同
(1) list不支持随机访问。假设要访问第五个元素, 就要顺着链表爬过前4个元素,因此,随机访问很慢
(2) 任何位置插入删除都很快
(3) 插入和删除操作, 不会导致指向其他元素的pointer,reference,iterator失效
(4) list对于异常的处理方式:要么操作成功, 要么什么都不发生。不会有只成功一般的现象
list所提供的成员函数反应出它与array,vector,deque的不同
(1) list提供front(), push_front(), pop_front(), back(), push_back(), pop_back()等操作
(2) 不支持随机访问, list既不提供下标操作, 也不提供at()
(3) list不提供容量、空间重新分配等操作,每个元素有自己的内存,在元素被删除前一直有效
(4) list提供不少特殊成员函数,专门用于移动和移除元素。较之同名的STL通用算法,这些函数执行起来更快,它无须复制或搬移元素,只需调整若干pointer。
部分操作列表
c.empty() | 是否为空 |
c.size() | 元素总量 |
c.max_size() | |
c1 == c2 | 每个元素都做对比 |
c1 != c2 | |
c1 < c2 | |
c1 > c2 | |
c1 <= c2 | |
c1 >= c2 | |
c = c2 | |
c = rv | |
c = initlist | |
c.assign(n,elem) | |
c.assign(beg,end) | |
c.assign(initlist) | |
c1.swap(c2) | |
swap(c1,c2) | |
c.front() | 返回第一个元素 |
c.back() | 返回最后一个元素 |
c.push_back(elem) | |
c.pop_back() | |
c.push_front(elem) | |
c.pop_front() | |
c.insert(pos,elem) | |
c.insert(pos,n,elem) | |
c.insert(pos,beg,end) | |
c.insert(pos,initlist) | |
c.emplace(pos,args...) | |
c.emplace_back(args...) | |
c.emplace_front(args...) | |
c.erase(pos) | |
c.erase(beg,end) | |
c.remove(val) | 删除所有值与val相等的元素 |
c.remove_if(op) | 对于op(elem)返回true的元素全部删除 |
c.resize(num) | |
c.resize(num,elem) | |
c.clear() | 清空列表 |
c.unique() | 如果存在若干相邻而数值相同的元素,就移除重复元素只留一个 |
c.unique(op) | 如果存在若干相邻元素都是op的结果为true,则移除重复元素只留一个 |
c.splice(pos,c2) | 将C2内所有元素移动到c内,迭代器pos之前 |
c.splice(pos,c2,c2pos) | 将C2内c2pos所指元素转移到c内pos所指位置 |
c.splice(pos,c2,c2beg,c2end) | 将c2内[c2beg,c2end)区间内所有元素转移到c内pos之前 |
c.sort() | 以operator<为准则对所有元素排序 |
c.sort(op) | 以op()为准则对所有元素排序 |
c.merge(c2) | 假设c和c2容器都包含op准则下已排序元素,将c2全部元素转移到c,并保准合并后仍已排序 |
c.merge(c2,op) | 假设c和c2都包含已排序元素,将c2全部元素转移到c,并保准合并后的list在op准则下仍已排序 |
c.reverse() | 将所有元素反序 |
使用示例
#include <list>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
void printLists (const list<int>& l1, const list<int>& l2)
{
cout << "list1: ";
copy (l1.cbegin(), l1.cend(), ostream_iterator<int>(cout," "));
cout << endl << "list2: ";
copy (l2.cbegin(), l2.cend(), ostream_iterator<int>(cout," "));
cout << endl << endl;
}
int main()
{
// create two empty lists
list<int> list1, list2;
// fill both lists with elements
for (int i=0; i<6; ++i) {
list1.push_back(i);
list2.push_front(i);
}
printLists(list1, list2);
// insert all elements of list1 before the first element with value 3 of list2
// - find() returns an iterator to the first element with value 3
list2.splice(find(list2.begin(),list2.end(), // destination position
3),
list1); // source list
printLists(list1, list2);
// move first element of list2 to the end
list2.splice(list2.end(), // destination position
list2, // source list
list2.begin()); // source position
printLists(list1, list2);
// sort second list, assign to list1 and remove duplicates
list2.sort();
list1 = list2;
list2.unique();
printLists(list1, list2);
// merge both sorted lists into the first list
list1.merge(list2);
printLists(list1, list2);
}
------------------------------------------------
list1: 0 1 2 3 4 5
list2: 5 4 3 2 1 0
list1:
list2: 5 4 0 1 2 3 4 5 3 2 1 0
list1:
list2: 4 0 1 2 3 4 5 3 2 1 0 5
list1: 0 0 1 1 2 2 3 3 4 4 5 5
list2: 0 1 2 3 4 5
list1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
list2: