
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
void ConstructionAndCopyConstructor()
{
list <int> lt1;
list <int> lt2(5, 10); // 使用5个10初始化容器
for (auto e : lt2)
{
cout << e << " ";
}
cout << endl;
list <int> lt3(lt2); // 拷贝构造函数
for (auto e : lt3)
{
cout << e << " ";
}
cout << endl;
list <int> lt4(lt2.begin(), lt2.end()); // 迭代器区间初始化
for (auto e : lt4)
{
cout << e << " ";
}
cout << endl;
int arr[] = { 1, 2, 3, 4, 5 };
int sz = sizeof(arr) / sizeof(int);
list<int> lt5(arr, arr + sz); //构造数组某段区间的复制品
for (auto e : lt5)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
ConstructionAndCopyConstructor();
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
void InsertionAndDeletion()
{
list<int> lt;
lt.push_front(1);
lt.push_front(2);
lt.push_front(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.pop_front();
lt.pop_front();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
InsertionAndDeletion();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
void InsertionAndDeletion()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.pop_back();
lt.pop_back();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
InsertionAndDeletion();
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void InsertAndErase()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator pos = lt.begin();
lt.insert(pos, 25); // 在pos位置插入25
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.insert(pos, 2, 5); // 在pos位置插入2个5
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
vector<int> v(2,6);
lt.insert(pos, v.begin(), v.end()); // 在pos位置插入2个6
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
InsertAndErase();
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void InsertAndErase()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator pos = lt.begin();
lt.insert(pos, 25); // 在pos位置插入25
lt.insert(pos, 2, 5); // 在pos位置插入2个5
vector<int> v(2,6);
lt.insert(pos, v.begin(), v.end()); // 在pos位置插入2个6
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
//在迭代器区间内寻找第一个值为3的元素并返回迭代器
pos = find(lt.begin(), lt.end(), 3);
lt.erase(pos); // 删除pos位置的元素
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
//在迭代器区间内寻找第一个值为5的元素并返回迭代器
pos = find(lt.begin(), lt.end(), 5);
// 删除pos位置到末尾的所有元素
lt.erase(pos, lt.end());
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
InsertAndErase();
return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestIterator()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
// 正向迭代器
cout << "正向迭代器遍历: ";
list<int>::iterator it = lt.begin();
while(it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
int main()
{
TestIterator();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestIterator()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
// 正向迭代器
cout << "正向迭代器遍历: ";
list<int>::iterator it = lt.begin();
while(it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//反向迭代器
cout << "反向迭代器遍历: ";
list<int>::reverse_iterator rit = lt.rbegin();
while (rit != lt.rend())
{
cout << *rit << " ";
++rit;
}
}
int main()
{
TestIterator();
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestElementAndAcquisition()
{
list<int> lt;
lt.push_back(10);
lt.push_back(20);
list<int>::iterator pos = lt.begin();
lt.insert(pos, 2, 8);
cout << "第一个元素: " << lt.front() << endl;
cout << "最后一个元素: " << lt.back() << endl;
}
int main()
{
TestElementAndAcquisition();
return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestControlCapacity()
{
list<int> lt;
lt.push_back(1);
vector<int> v(3, 5);
//在尾部插入3个5
lt.insert(lt.end(), v.begin(), v.end());
cout << "当前元素个数: " << lt.size() << endl;
cout << "是否为空: " << (lt.empty() ? "是" : "否") << endl;
lt.clear();
cout << "当前元素个数: " << lt.size() << endl;
cout << "是否为空: " << (lt.empty() ? "是" : "否") << endl;
}
int main()
{
TestControlCapacity();
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestControlCapacity()
{
list<int> lt;
lt.push_back(1);
lt.resize(5, 10);
cout << "当前元素个数: " << lt.size() << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.resize(3);
for (auto e : lt)
{
cout << e << " ";
}
}
int main()
{
TestControlCapacity();
return 0;
}
sort函数可以将容器当中的数据默认排为升序,不过效率比库中的sort要差很多.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestSort()
{
srand(time(0));
const int N = 1000000;
list<int> lt1;
list<int> lt2;
vector<int> v;
for (int i = 0; i < N; ++i)
{
auto e = rand() + i;
lt1.push_back(e);
v.push_back(e);
}
int begin1 = clock();
//vector排序
sort(v.begin(), v.end());
int end1 = clock();
int begin2 = clock();
//sort的list排序
lt1.sort();
int end2 = clock();
printf("vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
int main()
{
//TestControlCapacity();
TestSort();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestSort()
{
srand(time(0));
const int N = 1000000;
list<int> lt1;
list<int> lt2;
for (int i = 0; i < N; ++i)
{
auto e = rand();
lt1.push_back(e);
lt2.push_back(e);
}
int begin1 = clock();
// vector
vector<int> v(lt2.begin(), lt2.end());
//
sort(v.begin(), v.end());
// lt2
lt2.assign(v.begin(), v.end());
int end1 = clock();
int begin2 = clock();
lt1.sort();
int end2 = clock();
printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
int main()
{
TestSort();
return 0;
}

splice函数用于两个list容器之间的拼接,其有三种拼接方式:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestSplice()
{
list<int> lt1(4, 2);
list<int> lt2(4, 5);
lt1.splice(lt1.begin(), lt2); // 将lt2整体插入到lt1的begin位置
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
list<int> lt3(4, 8);
list<int> lt4(4, 9);
// 将lt4的第一个元素插入到lt3的begin位置
lt3.splice(lt3.begin(), lt4, lt4.begin());
for (auto e : lt3)
{
cout << e << " ";
}
cout << endl;
list<int> lt5(4, 3);
list<int> lt6(4, 4);
lt5.splice(lt5.begin(), lt6, lt6.begin(), lt6.end()); // 将lt6的所有元素插入到lt5的begin位置
for (auto e : lt5)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
TestSplice();
return 0;
}
PS:容器当中被拼接到另一个容器的数据在原容器当中就不存在了。(实际上就是将链表当中的指定结点拼接到了另一个容器当中)


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestRemove()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(2);
lt.push_back(4);
lt.push_back(2);
lt.push_back(5);
cout << "原始元素: ";
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.remove(2); // 删除所有值为2的元素
cout << "删除值为2后的元素: ";
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.remove_if([](int val) {return val % 2 == 1; }); // 删除所有奇数元素
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
TestRemove();
return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestUniqueAndMerge()
{
list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(2);
lt1.push_back(3);
lt1.push_back(3);
lt1.push_back(3);
cout << "原始元素: ";
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
lt1.unique(); // 删除相邻重复元素
cout << "删除相邻重复元素后: ";
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
list<int> lt2;
lt2.push_back(1);
lt2.push_back(3);
lt2.push_back(5);
list<int> lt3;
lt3.push_back(2);
lt3.push_back(4);
lt3.push_back(6);
lt2.merge(lt3); // 将lt3合并到lt2,要求两个容器有序
cout << "合并后的元素: ";
for (auto e : lt2)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
TestUniqueAndMerge();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestReverseAndSwap()
{
list<int> lt1;
lt1.push_back(1);
lt1.push_back(2);
lt1.push_back(3);
cout << "原始元素: ";
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
lt1.reverse(); // 反转容器元素
cout << "反转后元素: ";
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
list<int> lt2;
lt2.push_back(4);
lt2.push_back(5);
lt2.push_back(6);
cout << "交换前lt1元素: ";
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
cout << "交换前lt2元素: ";
for (auto e : lt2)
{
cout << e << " ";
}
cout << endl;
lt1.swap(lt2); // 交换两个容器的元素
cout << "交换后lt1元素: ";
for (auto e : lt1)
{
cout << e << " ";
}
cout << endl;
cout << "交换后lt2元素: ";
for (auto e : lt2)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
TestReverseAndSwap();
return 0;
}
assign函数用于将新内容分配给容器,替换其当前内容,新内容的赋予方式有两种:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <vector>
void TestAssign()
{
list<int> lt;
lt.assign(5, 10);
cout << "assign后元素: ";
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
vector<int> v = { 1, 2, 3, 4, 5 };
lt.assign(v.begin(), v.end()); // 使用v的元素填充容器
cout << "assign后元素: ";
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
TestAssign();
return 0;
}