二、list容器
1、基本概念
#include<iostream> #include<list> using namespace std; //list void print(list<int>&l) { for(list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout<<*it<<" "; } cout<<endl; } void test01() { list<int>l; //添加数据 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); print(l); //区间方式构造 list<int>l2(l.begin(),l.end() ); print(l2); //拷贝构造 list<int>l3(l2); print(l3); //n个m list<int>l4(14,12); print(l4); } int main() { test01(); return 0; }
2、排序案例
#include<iostream> #include<list> #include<string> using namespace std; //list //按年龄升序,如果年龄相同按身高降序排列。 class person { public: person(string name,int age,int height) { this->nname=name; this->nage=age; this->nheight=height; } public: string nname; int nage; int nheight; }; bool mycompare(person &p1,person &p2) { if(p1.nage==p2.nage) { return p1.nheight>p2.nheight; } return p1.nage<p2.nage; } void test01() { list<person>l; person p1("小名",18,175); person p2("小一",15,180); person p3("小二",13,126); person p4("小三",18,181); person p5("小四",15,175); l.push_back(p1); l.push_back(p2); l.push_back(p3); l.push_back(p4); l.push_back(p5); cout<<"排序前"<<endl; for(list<person>::iterator it=l.begin(); it!=l.end(); it++) { cout<<"姓名:"<<(*it).nname<<",年龄:"<<(*it).nage<<",身高:"<<(*it).nheight<<"。"<<endl; } cout<<"---------------------------------------------------------------------"<<endl; cout<<"排序后"<<endl; l.sort(mycompare); for(list<person>::iterator it=l.begin(); it!=l.end(); it++) { cout<<"姓名:"<<(*it).nname<<",年龄:"<<(*it).nage<<",身高:"<<(*it).nheight<<"。"<<endl; } } int main() { test01(); return 0; }
3、容器大小
#include<iostream> #include<list> using namespace std; //list void print(list<int>&l) { for(list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout<<*it<<" "; } cout<<endl; } void test01() { list<int>l; //添加数据 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); print(l); if(l.empty() ) { cout<<"容器为空"<<endl; } else { cout<<"容器不为空"<<endl; cout<<"容器中元素个数:"<<l.size()<<endl; } l.resize(10); cout<<"容器大小为:"<<l.size()<<endl; print(l); l.resize(3); cout<<"容器大小为:"<<l.size()<<endl; print(l); } int main() { test01(); return 0; }
4、赋值与交换
#include<iostream> #include<list> using namespace std; //list void print(list<int>&l) { for(list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout<<*it<<" "; } cout<<endl; } void test01() { list<int>l; //添加数据 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); print(l); list<int>l2; l2=l; print(l2); //assign list<int>l3; l3.assign(l.begin(),l.end()); print(l3); list<int>l4; l4.assign(18,12); print(l4); } void test02() { list<int>l; //添加数据 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); list<int>l2; l2.assign(18,12); cout<<"交换前"<<endl; print(l); print(l2); cout<<"交换后"<<endl; l.swap(l2); print(l); print(l2); } int main() { test01(); test02(); return 0; }
5、排序与反转
#include<iostream> #include<list> using namespace std; //list bool mycompare(int v1,int v2) { return v1>v2; } void print(list<int>&l) { for(list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout<<*it<<" "; } cout<<endl; } void test01() { list<int>l; //添加数据 l.push_back(1); l.push_back(45); l.push_back(34); l.push_back(67); print(l); //反转 l.reverse(); print(l); //排序 // 所有不支持随机访问的容器,都不能使用标准算法 //不支持随机访问的容器,内部会提供对应一些算法 l.sort(); print(l); l.sort(mycompare); print(l); } int main() { test01(); return 0; }
6、插入和删除
#include<iostream> #include<list> using namespace std; //list void print(list<int>&l) { for(list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout<<*it<<" "; } cout<<endl; } void test01() { list<int>l; //添加数据 尾加法 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); print(l); //2、头插 l.push_front(100); l.push_front(200); l.push_front(300); print(l); //3.尾删 l.pop_back() ; print(l); //4、头删 l.pop_front() ; print(l); //5、插入 l.insert(l.begin(),1000); print(l); //6、移动插入 list<int>::iterator it=l.begin() ; l.insert(++it,1000); print(l); //7、删除 list<int>::iterator iit=l.begin() ; l.erase(iit);//l.erase(++it) print(l); //8、移除 l.push_back(10000); l.push_back(10000); l.push_back(10000); l.push_back(10000); print(l); l.remove(10000);//删除所有与目标相匹配的元素 print(l); //9、清空 l.clear() ; print(l); } int main() { test01(); return 0; }
7、数据存取
#include<iostream> #include<list> using namespace std; //list void print(list<int>&l) { for(list<int>::iterator it=l.begin(); it!=l.end(); it++) { cout<<*it<<" "; } cout<<endl; } void test01() { list<int>l; //添加数据 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); print(l); cout<<"list中第一个元素为"<<l.front() <<endl;//list迭代器不支持随机访问,即it++成立 ,it=it+2不成立 cout<<"list中倒数第一个元素为"<<l.back() <<endl;//不可用l[]和l.at()访问,原因是l本质上是一个链表 } int main() { test01(); return 0; }
8、总结
标签:begin,list,back,c++,print,第三十七,push,打卡,include From: https://www.cnblogs.com/zzqq1314/p/17432967.html