4. 数据修改
由于链表结构的特殊性,我们可以很方便的头插,尾插或者在任意位置插入,所以在这里库里面对于数据插入提供了很多种方式
1.数据插入
1. push_back:尾插
2. push_front:头插
3.insert:在任意位置插入删除
void Test_insert() { vector<int> v(5, 888); list<int> lt; //尾插 lt.push_back(1); lt.push_back(2); lt.push_back(3); lt.push_back(4); lt.push_back(5); lt.push_back(6); auto it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; //头插 lt.push_front(10); it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; //在任意位置插入 auto it_push = ++lt.begin();//从第二个位置开始 lt.insert(it_push, 30);//插入一个值 it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; ++it_push; lt.insert(it_push, 5, 50);//插入n个值 it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; ++it_push; lt.insert(it_push, v.begin(), v.end());//插入一个迭代器区间 it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; }
2. 数据删除
与数据插入相对应的,数据删除也有三个
1. pop_back:尾删
2.pop_front:头删
3.erase:任意位置删除
void Test_erase() { list<int> lt; for (int i = 0; i < 10; ++i) { lt.push_back(i); } auto it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; //头删 lt.pop_front(); it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; //尾删 lt.pop_back(); it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; //任意位置删除 auto pos = ++lt.begin(); pos = lt.erase(pos);//删除某一位置 it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; auto start = ++pos; auto end = ++(++start); lt.erase(start, end);//删除一个迭代器区间 it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; }
5.其他接口
除了上述的接口之外,还有一些我们之前在string和vector中没有见到的接口,下面我们来看看他们的用法
1. remove:删除list中指定值
void Test_remove() { list<int> lt; lt.push_back(1); lt.push_back(2); lt.push_back(3); lt.push_back(4); lt.push_back(5); lt.push_back(6); lt.push_back(7); lt.push_back(8); auto it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; lt.remove(5); lt.remove(10); it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; }
可以看到,remove对于list中不存在的元素不会做任何操作
2. sort:排序list
看到这里肯定会有人有疑惑,sort算法库里面不是实现过吗?为什么又要重新在list里实现一下,我直接用算法库里面的不行吗?
✅答案是:不行,接下来看实验==》
可以看到,在编译的过程就已经报错了,这是因为库里面没有支持list迭代器类型的构造,为啥嘞?因为之前sort对容器内部的元素操作使用了+和-操作,但是list由于结构的限制,不支持迭代器的这个行为,所以对于list,要重新在库里面实现一个sort。
3. unique:删除list中的重复值
这里注意一下,在使用unique之前要确保list是有序的,否则不能完成删除所有重复值的功能
void Test_Sort() { list<int> lt; lt.push_back(1); lt.push_back(10); lt.push_back(9); lt.push_back(3); lt.push_back(6); lt.push_back(3); lt.push_back(7); lt.push_back(6); cout << "原list:"; auto it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; lt.unique(); cout << "尝试在乱序的情况下使用unique:"; it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; lt.sort(); cout << "排序list:"; it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; cout << "对有序的list使用unique:"; lt.unique(); it_out = lt.begin(); while (it_out != lt.end()) { cout << *it_out << " "; ++it_out; } cout << endl; }