目录
[一 list 常见构造](#一 list 常见构造)
[1 空容器构造函数(默认构造函数)](#1 空容器构造函数(默认构造函数))
[2 Fill 构造函数](#2 Fill 构造函数)
[3 Range 构造函数](#3 Range 构造函数)
[4 拷贝构造函数](#4 拷贝构造函数)
[二 list迭代器](#二 list迭代器)
[1 begin && end](#1 begin && end)
[2 rbegin && rend](#2 rbegin && rend)
[三 list 容量操作](#三 list 容量操作)
[四 list 修改操作](#四 list 修改操作)
[1 assign](#1 assign)
[2 push_front && pop_back](#2 push_front && pop_back)
[3 push_back && pop_back](#3 push_back && pop_back)
[4 insert](#4 insert)
[5 erase](#5 erase)
[6 swap](#6 swap)
[7. resize](#7. resize)
[8 clear](#8 clear)
[9 关于 emplace系列操作说明](#9 关于 emplace系列操作说明)
[五 list 元素访问](#五 list 元素访问)
[六 其他操作](#六 其他操作)
[1 splice](#1 splice)
[2 remove](#2 remove)
[3 unique](#3 unique)
[4 sort](#4 sort)
[5 reverse](#5 reverse)
[七 总结](#七 总结)
一 list 常见构造
1 空容器构造函数(默认构造函数)
构造一个没有元素的空容器。
2 Fill 构造函数
构造一个包含 n 个元素的容器。每个元素都是 val 的副本。
3 Range 构造函数
构造一个容器,其中包含与范围 [first,last] 一样多的元素,每个元素都按相同的顺序从该范围中的相应元素构造而成。
4 拷贝构造函数
构造一个容器,其中包含 x 中每个元素的副本,顺序相同。
cpp
#include<iostream>
#include<list>
using namespace std;
void Print(list<int>& lt)
{
list<int>:: iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
int main()
{
list<int> a;
list<int> b(4, 100);
list<int> c(4);
list<int> d(b.begin(), b.end());
list<int> e(b);
cout << "b: ";
Print(b);
cout << "c: ";
Print(c);
cout << "d: ";
Print(d);
cout << "e: ";
Print(e);
return 0;
}
C++11构造
cpp
void Print(list<int>& lt)
{
list<int>:: iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
int main()
{
list<int> lt1{ 1, 2, 3, 4, 5 };
Print(lt1);
return 0;
}
![](https://file.jishuzhan.net/article/1794208074672640002/e42610ece373982f5c8e27694d41577b.webp)
二 list迭代器
list模式和vector如出一辙
注意:
ist的迭代器是支持++
和--
操作的(前后缀都支持)
但是不支持+
和-
操作,因为链表不能进行随机访问操作,只能O(n)
查询
1 begin && end
cpp
iterator begin();const_iterator begin() const;
iterator end();const_iterator end() const;
cpp
void Test3()
{
list<int> lt(4, 1);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
const list<int> llt(4, 10);
list<int>::const_iterator itt = llt.begin();
while (itt != llt.end())
{
cout << *itt << ' ';
++itt;
}
}
![](https://file.jishuzhan.net/article/1794208074672640002/ced0feef49768ac018ef01dbb60926dc.webp)
2 rbegin && rend
cpp
reverse_iterator rbegin();const_reverse_iterator rbegin() const;
reverse_iterator rend();const_reverse_iterator rend() const;
cpp
void Test4()
{
list<int> lt(5);
list<int>::reverse_iterator it = lt.rbegin();
int i = 1;
while (it != lt.rend())
{
*it += i;
++it;
++i;
}
for (auto e : lt)
{
cout << e << ' ';
}
}
![](https://file.jishuzhan.net/article/1794208074672640002/42988a4c6a81d983ae4a86b513ddc392.webp)
三 list 容量操作
![](https://file.jishuzhan.net/article/1794208074672640002/50a0bf13fbfc8de3aaa881d3d3f40a6e.webp)
这里直接代码演示
cpp
void Test5()
{
list<int> lt1;
if (lt1.empty()) cout << "empty" << endl;
else cout << "full" << endl;
list<int> lt2{ 1, 2, 3, 4 };
cout << "lt1: " << lt1.size() << " lt2: " << lt2.size() << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/a15f4900df011c93ff7477259362b8fd.webp)
四 list 修改操作
![](https://file.jishuzhan.net/article/1794208074672640002/d1a3dbec08abd973798f1254aaae5385.webp)
1 assign
将新内容分配给 list 容器,替换其当前内容,并相应地修改其大小。
cpp
range (1)
template <class InputIterator> void assign (InputIterator first, InputIterator last);
fill (2)
void assign (size_type n, const value_type& val);
cpp
void Test6()
{
std::list<int> first;
std::list<int> second;
first.assign(7, 100);
second.assign(first.begin(), first.end());
int myints[] = {1776,7,4 };
first.assign(myints, myints + 3);
cout << "first: ";
for (auto e : first)
{
cout << e << " ";
}
cout << endl;
cout << "second: ";
for (auto e : second)
{
cout << e << " ";
}
cout << endl;
}
2 push_front && pop_back
cpp
void push_front (const value_type& val);
void pop_front();
cpp
void Test7()
{
list<int> lt = { 1, 2 };
lt.push_front(100);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.pop_front();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
3 push_back && pop_back
cpp
void push_back (const value_type& val);
void pop_back();
cpp
void Test8()
{
list<int> lt = { 1, 2 };
lt.push_back(100);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.pop_back();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/f86766b07e9f4ab0d57c2b340a1f96b4.webp)
4 insert
指定位置的元素之前插入新元素来扩展容器,从而有效地通过插入的元素数增加容器大小
cpp
single element (1)
iterator insert (iterator position, const value_type& val);
fill (2)
void insert (iterator position, size_type n, const value_type& val);
range (3)
template <class InputIterator> void insert (iterator position, InputIterator first, InputIterator last);
cpp
void Test9()
{
list<int> lt(4, 10);
lt.insert(lt.begin(), 100);
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl;
lt.insert(lt.end(), 4, 100);
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/db9f5a566138d88f4444aca82c5fa1bc.webp)
5 erase
从容器中删除单个元素 (position) 或一系列元素 ( [first,last))
cpp
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
cpp
void Test10()
{
list<int> lt{ 1, 2, 3, 4, 5, 6};
lt.erase(lt.begin());
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl;
lt.erase(++lt.begin(), lt.end());
for (auto e : lt)
{
cout << e << ' ';
}
}
![](https://file.jishuzhan.net/article/1794208074672640002/04dba6d58404875a7fe2cda95cf11483.webp)
6 swap
通过 x 的内容交换容器的内容,x 是另一个相同类型的容器对象。size可能有所不同。
cpp
void swap (list& x);
cpp
void Test11()
{
list<int> l1(4, 10);
list<int> l2(4, 1);
l1.swap(l2);
cout << "l1: ";
for (auto e : l1)
{
cout << e << ' ';
}
cout << endl;
cout << "l2: ";
for (auto e : l2)
{
cout << e << ' ';
}
}
![](https://file.jishuzhan.net/article/1794208074672640002/da5b2ed24eb2e6e99fe813ea4da82c35.webp)
7. resize
调整容器的大小,使其包含 n 个元素。
cpp
void resize (size_type n);
void resize (size_type n, const value_type& val);
如果 n 小于当前容器大小,则内容将减少到其前 n 个元素,删除超出(并销毁它们)的元素。
如果 n 大于当前容器大小,则通过在末尾插入所需数量的元素来扩展内容,以达到 n 的大小。
如果指定了 val ,则新元素将初始化为 val 的副本,否则,它们将进行值初始化(0)。
cpp
void Test12()
{
list<int> mylist;
for (int i = 1; i < 10; ++i) mylist.push_back(i);
mylist.resize(5);
cout << "mylist contains:";
for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
cout << endl;
mylist.resize(8, 100);
cout << "mylist contains:";
for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
cout << endl;
mylist.resize(12);
cout << "mylist contains:";
for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
cout << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/88017b5d7f3dd962cd1c906cc6b1f10d.webp)
8 clear
从容器中删除所有元素(这些元素被销毁),使容器的大小为0。
cpp
void clear() noexcept;
cpp
void Test13()
{
list<int> lt(1, 4);
lt.clear();
cout << lt.size() << endl;
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/69b7912780e80e5c86e550255c448a70.webp)
9 关于 emplace系列操作说明
这个系列的操作放在 右值引用讲解完之后再讲, 这样才能更好的理解. 大家不需要着急的, 因为 上面讲的完全够用, 而且emplace 是优化, 并不是其他功能
五 list 元素访问
![](https://file.jishuzhan.net/article/1794208074672640002/8ae6bd3666e3d2a51aff95e958a9ffa4.webp)
cpp
void Test13()
{
list<int> lt{ 1, 2, 3, 4 };
cout << lt.front() << endl;
cout << lt.back() << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/79746089abea16f5c324669eaf1749c0.webp)
六 其他操作
![](https://file.jishuzhan.net/article/1794208074672640002/5b84c6896b9c2261f0aa0498fc0467e2.webp)
1 splice
将元素从一个列表转移到另一个列表
cpp
void splice(iterator position, list& x);
将 x 的所有元素转移到容器中
void splice(iterator position, list& x, iterator i);
仅将 i 指向的元素从 x 传输到容器中
void splice(iterator position, list& x, iterator first, iterator last);
将范围 [first,last) 从 x 传输到容器中。
cpp
void Test14()
{
list<int> mylist1 = { 1, 2, 3, 4 }, mylist2 = { 10, 20, 30 };
list<int>::iterator it = mylist1.begin();
++it; // points to 2
mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
cout << "mylist1: ";
for (auto e : mylist1)
{
cout << e << " ";
}
cout << endl;
cout << "mylist2: ";
for (auto e : mylist2)
{
cout << e << " ";
}
cout << endl;
mylist2.splice(mylist2.begin(), mylist1, mylist1.begin());
cout << "mylist1: ";
for (auto e : mylist1)
{
cout << e << " ";
}
cout << endl;
cout << "mylist2: ";
for (auto e : mylist2)
{
cout << e << " ";
}
cout << endl;
mylist1.splice(mylist1.begin(), mylist2, mylist2.begin(), mylist2.end());
cout << "mylist1: ";
for (auto e : mylist1)
{
cout << e << " ";
}
cout << endl;
cout << "mylist2: ";
for (auto e : mylist2)
{
cout << e << " ";
}
cout << endl;
}
![](https://file.jishuzhan.net/article/1794208074672640002/0d0f7c9bd882db59d53fdba5212ddb88.webp)
2 remove
从容器中删除所有与 val 相等的元素。这将调用这些对象的析构函数,并通过删除的元素数来减小容器大小。
cpp
vvoid remove (const value_type& val);
cpp
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(2);
lt.push_back(4);
lt.push_back(5);
cout << lt.size() << endl;
lt.remove(2);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
cout << lt.size() << endl;
return 0;
}
![](https://file.jishuzhan.net/article/1794208074672640002/b17edfae431501adc6588c2a82b0f5bd.webp)
3 unique
从容器中每个连续的相等元素组中删除除第一个元素之外的所有元素。请注意,只有当某个元素与其前面的元素相等时,该元素才会从列表容器中删除。因此,此函数对于排序列表特别有用。
cpp
void unique();
cpp
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(1);
lt.push_back(2);
lt.push_back(4);
lt.push_back(2);
lt.push_back(2);
lt.push_back(4);
lt.push_back(3);
lt.push_back(5);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.sort();//需要先排序
lt.unique();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
![](https://file.jishuzhan.net/article/1794208074672640002/332c5dde45a2a6a9906a656157899bef.webp)
4 sort
对容器中的元素进行排序
对列表 中的元素进行排序,改变它们在容器中的位置。
cpp
void sort();
cpp
void Test15()
{
list<int> lt{ 2, 1, 2, 4, 10, 11, 99, 88 };
lt.sort();
for (auto e : lt)
{
cout << e << " ";
}
}
![](https://file.jishuzhan.net/article/1794208074672640002/62b8c234dac80b6ae6535f6ececd474c.webp)
5 reverse
反转列表容器 中元素的顺序
cpp
void reverse()void reverse()
cpp
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.reverse();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
![](https://file.jishuzhan.net/article/1794208074672640002/d54ba384af05f5ae777a20fe4855197b.webp)
七 总结
list终于抽时间写完了, 首先本节大多数代码实验, 理论讲解少, 还是那句话, 如果对概念不清楚, 请到 string看, 或者翻阅文档, 这些都是最基本的素养. list 很多操作和 vector一样的, 但也有差别, 注意区分即可.
今天去中医看病, 医生说 眼睛就是你的肾, 舌头是你的心脏, 大家一定注意饮食规律, 不要久坐啊.