1.简介
list:带头双向循环链表
2.遍历方式
cpp
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
//遍历方式,不再支持[]
//当然是可以支持的,但效率过低
list<int>::iterator it = lt.begin();
//迭代器是属于内嵌类型,一般使用typedef或是内部类的方式实现
//list的迭代器更加复杂,就不是原生指针了
while (it != lt.end())
{
cout << *(it++) << " ";
}
cout << endl;
//底层就是迭代器,编译会器把范围for替换成迭代器
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
3.构造
4. Modifiers
4.1 assign
4.2 insert
5. Operations
5.1 reverse
cpp
void test_list2()
{
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;
}
5.2 sort
为什么算法库中有sort ,list还要单独写个sort
算法库的sort
list的sort
cpp
void test_list2()
{
list<int> lt;
lt.push_back(5);
lt.push_back(4);
lt.push_back(3);
lt.push_back(2);
lt.push_back(1);
//sort(lt.begin(), lt.end());//使用时要包含头文件<algorithm>
lt.sort();//默认是升序
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
//升序 < less
//如果想排降序 > greater
// 1.
//greater<int> gt;
//lt.sort(gt);
//2.
lt.sort(greater<int>());//使用匿名对象
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
5.2.1比较库中与list中sort的性能差异
cpp
//注意:测试性能时要切换为release
void test_op()
{
srand(time(0));
const int N = 100000;
vector<int> v;
list<int> lt1;
for (int i = 0; i < N; ++i)
{
auto e = rand();
lt1.push_back(e);
v.push_back(e);
}
//1.库中的sort
int begin1 = clock();
sort(v.begin(),v.end());
int end1 = clock();
//2.list的sort
int begin2 = clock();
lt1.sort();
int end2 = clock();
printf("vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
结论:库中的sort性能更好
cpp
//实际上,list的排序是很无用的
void test_op()
{
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();
//1.先拷贝到vector
vector<int> v(lt2.begin(),lt2.end());
//2.排序
sort(v.begin(),v.end());
//3.拷贝回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);
//哪怕是这样,库中的sort性能也更好
//也说明了拷贝数据的代价并没有很大
printf("list sort:%d\n", end2 - begin2);
}
list中sort的优势就是方便一些,数据量少时可以使用
5.3 merge
5.4 unique
cpp
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(2);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
lt.push_back(5);
lt.push_back(5);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.unique();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
5.5 remove
cpp
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(2);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
lt.push_back(5);
lt.push_back(5);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.remove(2);
lt.remove(30);
//如果remove一个不存在的值,就什么也不会做
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
5.6 splice
cpp
void test_list4()
{
std::list<int> mylist1, mylist2;
std::list<int>::iterator it;
// set some initial values:
for (int i = 1; i <= 4; ++i)
mylist1.push_back(i); // mylist1: 1 2 3 4
for (int i = 1; i <= 3; ++i)
mylist2.push_back(i * 10); // mylist2: 10 20 30
for (auto e : mylist1)
{
cout << e << " ";
}
cout << endl;
for (auto e : mylist2)
{
cout << e << " ";
}
cout << endl;
it = mylist1.begin();
++it; // points to 2
mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)
for (auto e : mylist1)
{
cout << e << " ";
}
cout << endl;
for (auto e : mylist2)
{
cout << e << " ";
}
cout << endl;
}