源文件
#include
#include
#include
#include
#include
using namespace std;
void printList(list l){
for (auto it = l.cbegin(); it != l.cend();it ++){
cout << *it << " ";
}
cout << endl;
}
void printStrList(list l){
for (auto it = l.cbegin(); it != l.cend();it ++){
cout << *it << " ";
}
cout << endl;
}
void createList(){
//way1
list l0;
list l1 {5,6,7,8,9};
printList(l1);
//way2
list l2(5);
printList(l2);
//way3
list l3(5,10);
printList(l3);
//way4
list l4(l2);
printList(l4);
//way5
array arr {11,22,33,44,55};
list l5(arr.cbegin()+2,arr.cend());
printList(l5);
}
void printValue(int value){
cout << value << " ";
}
void listTraverse(){
list l {11,22,33,44,55};
//way1
auto it = l.cbegin();
while (it != l.cend())
{
cout<< *it << " ";
it++;
}
cout << endl;
//way2
for (auto it = l.crbegin(); it != l.crend(); ++ it){
cout << *it << " ";
}
cout << endl;
//way3
for_each(l.rbegin(),l.rend(),printValue);
cout << endl;
//way4
for(auto value : l){
cout << value << " ";
}
cout << endl;
//way5
for_each(begin(l),end(l),printValue);
cout << endl;
}
void listGet(){
list l {11,22,33,44,55};
//way1 非迭代器方式
l.back() = 99;
cout << l.front()<<"~"< values{1,2,3};
values.push_front(0);//{0,1,2,3}
values.push_back(4); //{0,1,2,3,4}
values.emplace_front(-1);//{-1,0,1,2,3,4}
values.emplace_back(5); //{-1,0,1,2,3,4,5}
//emplace(pos,value),其中 pos 表示指明位置的迭代器,value为要插入的元素值
values.emplace(values.end(), 6);//{-1,0,1,2,3,4,5,6}
for (auto p = values.begin(); p != values.end(); ++p) {
cout << *p << " ";
}
}
void listInsert(){
std::list values{ 1,2 };
//第一种格式用法
values.insert(values.begin() , 3);//{3,1,2}
//第二种格式用法
values.insert(values.end(), 2, 5);//{3,1,2,5,5}
//第三种格式用法
std::arraytest{ 7,8,9 };
values.insert(values.end(), test.begin(), test.end());//{3,1,2,5,5,7,8,9}
//第四种格式用法
values.insert(values.end(), { 10,11 });//{3,1,2,5,5,7,8,9,10,11}
for (auto p = values.begin(); p != values.end(); ++p)
{
cout << *p << " ";
}
}
void listSplice(){
//创建并初始化 2 个 list 容器
list mylist1{ 1,2,3,4 }, mylist2{10,20,30};
list::iterator it = ++mylist1.begin(); //指向 mylist1 容器中的元素 2
//调用第一种语法格式
mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2:
// it 迭代器仍然指向元素 2,只不过容器变为了 mylist1
//调用第二种语法格式,将 it 指向的元素 2 移动到 mylist2.begin() 位置处
mylist2.splice(mylist2.begin(), mylist1, it); // mylist1: 1 10 20 30 3 4
// mylist2: 2
// it 仍然指向元素 2
//调用第三种语法格式,将 [mylist1.begin(),mylist1.end())范围内的元素移动到 mylist.begin() 位置处
mylist2.splice(mylist2.begin(), mylist1, mylist1.begin(), mylist1.end());//mylist1:
//mylist2:1 10 20 30 3 4 2
cout << "mylist1 包含 " << mylist1.size() << "个元素" << endl;
cout << "mylist2 包含 " << mylist2.size() << "个元素" << endl;
//输出 mylist2 容器中存储的数据
cout << "mylist2:";
for (auto iter = mylist2.begin(); iter != mylist2.end(); ++iter) {
cout << *iter << " ";
}
}
void listDelete1(){
listvalues{ 1,2,3,4 };
//删除当前容器中首个元素
values.pop_front();//{2,3,4}
//删除当前容器最后一个元素
values.pop_back();//{2,3}
//清空容器,删除容器中所有的元素
values.clear(); //{}
printList(values);
}
void listDelete2(){
list l {5,6,7,8,9};
l.erase(++l.begin());
printList(l);
l.erase(++l.cbegin(),--l.cbegin());
printList(l);
//remove()
list l2 {"how","is","are","is","you"};
l2.remove("is");
printStrList(l2);
//remove_if()
std::list l3{ 15, 36, 7, 17, 20, 39, 4, 1 };
//删除 mylist 容器中能够使 lamba 表达式成立的所有元素。
l3.remove_if([](int value) {return (value < 10); }); //{15 36 17 20 39}
printList(l3);
}
//二元谓词函数
bool demo(double first, double second)
{
return (int(first) == int(second));
}
void listUnique(){
list mylist{ 1,1.2,1.2,3,4,4.5,4.6 };
//1.删除相邻重复的元素,仅保留一份 remove()全移除
mylist.unique();//{1, 1.2, 3, 4, 4.5, 4.6}
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << *it << ' ';
cout << endl;
//2.demo 为二元谓词函数,是我们自定义的去重规则
mylist.unique(demo);
for (auto it = mylist.begin(); it != mylist.end(); ++it)
std::cout << *it << ' ';
}
int main(){
// createList();
// listTraverse();
// listGet();
// listInsertDel();
// listInsert();
// listSplice();
// listDelete1();
listDelete2();
listUnique();
return 0;
}