目录
- 一、list的介绍
- list的介绍
- 二、list的使用
- 2.1 list的构造函数
- 2.2 list迭代器的使用
- 2.3 list相关的容量大小相关的函数
- 2.4 list数据的访问相关的函数
- 2.5 list的数据调整相关的函数
- 2.6 list中其他函数操作
一、list的介绍
list的介绍
- list是可以以O(1)的时间复杂度任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
二、list的使用
2.1 list的构造函数
构造函数 | 接口说明 |
---|---|
list() | 空构造 |
list (size_type n, const value_type& val = value_type()) | 初始化的list中包含n个val值 |
list (const list& x) | 拷贝构造函数 |
list (InputIterator first, InputIterator last) | 用迭代器区间[first,last)构造list |
void test_list1() { // 空构造 list<int> l1; l1.push_back(1); l1.push_back(2); l1.push_back(3); l1.push_back(4); for (int e: l1) { cout << e << " "; } cout << endl; // 初始化的list中包含n个val值 list<int> l2(4,10); for (int e : l2) { cout << e << " "; } cout << endl; // 拷贝构造函数 list<int> l3(l1); for (int e : l3) { cout << e << " "; } cout << endl; // 用迭代器区间[first,last)构造list list<int> l4(l3.begin(), l3.end()); for (int e : l4) { cout << e << " "; } }
2.2 list迭代器的使用
函数声明 | 接口说明 |
---|---|
begin+end | 返回第一个元素的迭代器+返回最后一个元素的下一个位置的迭代器 |
rbegin+rend | 返回end位置+返回begin位置 |
// 正\反向迭代器 void test_list2() { list<int> lt; lt.push_back(1); lt.push_back(2); lt.push_back(3); lt.push_back(4); // 正向迭代器 list<int>::iterator it = lt.begin(); while (it!=lt.end()) { cout << *it << " "; ++it; } cout << endl; // 反向迭代器 list<int>::reverse_iterator rit = lt.rbegin(); while (rit!=lt.rend()) { cout << *rit << " "; ++rit; } }
2.3 list相关的容量大小相关的函数
函数声明 | 接口说明 |
---|---|
empty | 检测list是否为空,是返回true,否返回false |
size | 返回list中有效结点的个数 |
void test_list3() { list<int> l1; l1.push_back(1); l1.push_back(2); l1.push_back(3); l1.push_back(4); cout << l1.size() << endl; // 4 cout << l1.empty() << endl;// 0 }
2.4 list数据的访问相关的函数
函数声明 | 接口说明 |
---|---|
front | 返回list中的第一个结点值的引用 |
back | 返回list中最后一个结点值的引用 |
void test_list4() { list<int> l1; l1.push_back(1); l1.push_back(2); l1.push_back(3); l1.push_back(4); cout << l1.front() << endl; // 1 cout << l1.back() << endl; // 4 }
2.5 list的数据调整相关的函数
函数声明 | 接口说明 |
---|---|
push_front | 在首元素前插入元素 |
pop_front | 删除第一个元素 |
push_back | 尾插 |
pop_back | 尾删 |
insert | 在pos位置插入值 |
erase | 删除pos位置的值 |
swap | 交换两个list中的值 |
clear | 清空list中的有效元素 |
void test_list5() { list<int> l; l.push_back(1); l.push_front(2); list<int>::iterator it = l.begin(); ++it; l.insert(it, 20); for (int e : l) { cout << e << " "; } cout << endl; cout << "------" << endl; l.clear(); for (int e : l) { cout << e << " "; } }
2.6 list中其他函数操作
函数声明 | 接口说明 |
---|---|
sort | 排序 |
reverse | 逆置 |
unique | 去重(去重之前一般需要先排序) |
remove | 删除给定的一个值 |
void test_list6() { list<int> l1; l1.push_back(1); l1.push_back(7); l1.push_back(3); l1.push_back(3); l1.push_back(3); l1.push_back(4); // 排序 l1.sort(); for (int e : l1) { cout << e << " "; } cout << endl; // 逆置 l1.reverse(); for (int e : l1) { cout << e << " "; } cout << endl; // 去重 l1.unique(); for (int e : l1) { cout << e << " "; } cout << endl; // 删除给定的一个值 l1.remove(7); for (int e : l1) { cout << e << " "; } cout << endl; }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)