c++ STL之list对结构体的增加,删除,排序等操作详解

时间:2021-05-20

对STL中的list进一步学习,编程过程中对结构体的操作很多。

全部代码如下:

#include<iostream>#include<list>#include<string>#include<iterator>#include<algorithm>using namespace std;//变量及、初始化、重载***********************************************************************************typedef struct node//学生结构体{ int sno;//学生学号 string sname;//学生姓名 double sgrade;//学生成绩}Student; list<Student> s; list<Student>::iterator lit; list<Student>::reverse_iterator rlit; bool operator ==(const Student &stu1,const Student &stu2)//重载 == ,注意要用const类型 { bool flag=false; if(stu1.sno==stu2.sno&&stu1.sname==stu2.sname&&stu1.sgrade==stu2.sgrade)flag=true; return flag; }//菜单***********************************************************************************void menu()//总菜单{ cout<<"*************1.增加学生 2.删除学生*************"<<endl; cout<<"*************3.排序 4.显示第一个***********"<<endl; cout<<"*************5.显示最后一个 6.显示全部*************"<<endl; cout<<"*************7.退出"<<endl;}void menu_Add()//添加学生菜单{ cout<<"*************1.尾部增加一个学生 2.前部增加一个学生*************"<<endl; cout<<"*************3.尾部增加多个学生 4.前部增加多个学生*************"<<endl; cout<<"*************5.某位置前增加一个学生 6.某位置前增加多个相同学生*****"<<endl; cout<<"*************7.返回主菜单"<<endl;}void menu_Delete()//删除学生菜单{ cout<<"*************1.删除最后一个学生 2.删除第一个学生*************"<<endl; cout<<"*************3.删除某位学生 4.删除某学号的学生 **********"<<endl; cout<<"*************5.删除全部学生 6.返回主菜单"<<endl;}void menu_Sort()//删除学生菜单{ cout<<"*************1.按学号升序排序 2.按学号降序排序*************"<<endl; cout<<"*************3.按成绩升序排序 4.按成绩降序排序*************"<<endl; cout<<"*************5.返回主菜单"<<endl;}//函数***********************************************************************************void Display()//显示{ int choice; cout<<"*********0.正向显示 1.反向显示*************"<<endl; cin>>choice; if(choice==0) { if(s.empty())cout<<"当前没有学生!!!"<<endl; for(lit=s.begin();lit!=s.end();lit++) { cout<<"-------------------------------------------"<<endl; cout<<"学号:"<<(*lit).sno<<endl; cout<<"姓名:"<<(*lit).sname<<endl; cout<<"成绩:"<<(*lit).sgrade<<endl; cout<<"-------------------------------------------"<<endl; } } else { if(s.empty())cout<<"当前没有学生!!!"<<endl; for(rlit=s.rbegin();rlit!=s.rend();rlit++) { cout<<"-------------------------------------------"<<endl; cout<<"学号:"<<(*rlit).sno<<endl; cout<<"姓名:"<<(*rlit).sname<<endl; cout<<"成绩:"<<(*rlit).sgrade<<endl; cout<<"-------------------------------------------"<<endl; } }}void Add()//添加{ int choice,n,place;Student stu; while(1) { menu_Add(); cout<<"请输入序号:"<<endl; cin>>choice; if(7==choice)break; switch(choice) { case 1:{ cout<<"请输入学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; s.push_back(stu);Display(); }break; case 2:{ cout<<"请输入学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; s.push_front(stu);Display(); }break; case 3:{ cout<<"请输入要添加的学生个数:"<<endl; cin>>n; for(int i=0;i<n;i++) { cout<<"请输入第"<<i+1<<"个学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; s.push_back(stu); } Display(); }break; case 4:{ cout<<"请输入要添加的学生个数:"<<endl; cin>>n; for(int i=0;i<n;i++) { cout<<"请输入第"<<i+1<<"个学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; s.push_front(stu); } Display(); }break; case 5:{ cout<<"请输入要在哪个位置前添加(首元素位置为0):"<<endl; while(1) { cin>>place; if(place<0||place>s.size()){cout<<"位置输入错误!!!"<<endl;continue;} break; } lit=s.begin(); while(place) { lit++; place--; } cout<<"请输入学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; lit=s.insert(lit,stu); Display(); }break; case 6:{ cout<<"请输入要在哪个位置前添加(首元素位置为0):"<<endl; while(1) { cin>>place; if(place<0||place>s.size()){cout<<"位置输入错误!!!"<<endl;continue;} break; } lit=s.begin(); while(place) { lit++; place--; } cout<<"请输入学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; cout<<"请输入要插入的个数:"<<endl; cin>>n; s.insert(lit,n,stu); Display(); }break; default:cout<<"输入错误!!!"<<endl; } }}void Delete()//删除{ int choice;Student stu; while(1) { menu_Delete(); cout<<"请输入序号:"<<endl; cin>>choice; if(6==choice)break; switch(choice) { case 1:{ if(s.empty())cout<<"当前没有学生,无法删除"<<endl; else{ s.pop_back(); Display(); } }break; case 2:{ if(s.empty())cout<<"当前没有学生,无法删除"<<endl; else{ s.pop_front(); Display(); } }break; case 3:{ cout<<"请输入学生的学号、姓名、成绩:"<<endl; cin>>stu.sno>>stu.sname>>stu.sgrade; s.remove(stu); Display(); }break; case 4:{ int sno; cout<<"请输入学号:"<<endl; cin>>sno; for(lit=s.begin();lit!=s.end();)//此为重点,重载==不是个好办法 { if((*lit).sno==sno)lit=s.erase(lit);//删除并返回下一指针 else lit++; } Display(); }break; case 5:{ s.clear(); cout<<"已清空!!!"<<endl; }break; default:cout<<"输入错误!!!"<<endl; } }}bool cmp_sno_up(const Student &stu1,const Student &stu2)//学号升序预判定函数{ bool flag; stu1.sno<stu2.sno?flag=true:flag=false; return flag;}bool cmp_sno_down(const Student &stu1,const Student &stu2)//学号降序预判定函数{ bool flag; stu1.sno>stu2.sno?flag=true:flag=false; return flag;}bool cmp_sgrade_up(const Student &stu1,const Student &stu2)//学号升序预判定函数{ bool flag; stu1.sgrade<stu2.sgrade?flag=true:flag=false; return flag;}void Sort()//排序{ int choice;Student stu; while(1) { menu_Sort(); cout<<"请输入序号:"<<endl; cin>>choice; if(5==choice)break; switch(choice) { case 1:{ s.sort(cmp_sno_up);//按学号升序 Display(); }break; case 2:{ s.sort(cmp_sno_down);//按学号降序 Display(); }break; case 3:{ s.sort(cmp_sgrade_up);//按成绩升序 Display(); }break; case 4:{ s.sort(cmp_sgrade_up);//按成绩升序 s.reverse(); //反向,最终结果确实降序排列,按道理不该这么写, Display(); //为向读者展示更多函数使用才这么设计的 }break; case 5:{ s.clear(); cout<<"已清空!!!"<<endl; }break; default:cout<<"输入错误!!!"<<endl; } }}void Showfront()//显示首元素{ Student stu; if(s.empty())cout<<"当前没有学生!!!"<<endl; else { stu=s.front(); cout<<"-------------------------------------------"<<endl; cout<<"学号:"<<stu.sno<<endl; cout<<"姓名:"<<stu.sname<<endl; cout<<"成绩:"<<stu.sgrade<<endl; cout<<"-------------------------------------------"<<endl; }}void Showend()//显示尾元素,不能用end函数,end返回尾元素的下一个位置,这是迭代器规定{ Student stu; if(s.empty())cout<<"当前没有学生!!!"<<endl; else { rlit=s.rbegin(); cout<<"-------------------------------------------"<<endl; cout<<"学号:"<<(*rlit).sno<<endl; cout<<"姓名:"<<(*rlit).sname<<endl; cout<<"成绩:"<<(*rlit).sgrade<<endl; cout<<"-------------------------------------------"<<endl; }}//主函数*********************************************************************************int main(){ int choice; while(1) { menu(); cout<<"请输入序号:"<<endl; cin>>choice; if(7==choice){cout<<"欢迎再来!!!"<<endl;break;} switch(choice) { case 1:{ Add(); }break; case 2:{ Delete(); }break; case 3:{ Sort(); }break; case 4:{ Showfront(); }break; case 5:{ Showend(); }break; case 6:{ Display(); }break; default:cout<<"输入错误!!!"<<endl; } } return 0;}

结果截图:

尾部增加某

某位置前加

按学号删除

按成绩升序

按学号降序

显示最后一个元素

其余功能请读者复制原代码自行验证。

注意:

1.end()函数是返回尾元素的下一个,是空的。

2.反向遍历时用reverse_iterator, 不能用iterator,迭代器没有--

3.注意排序时预判定函数的编写

补充知识:C++ STL list的初始化、添加、遍历、插入、删除、查找、排序、释放

list是C++标准模版库(STL,Standard Template Library)中的部分内容。

实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。

使用list容器之前必须加上STL的list容器的头文件:

#include<list>;

list属于std命名域的内容,因此需要通过命名限定:

using std::list;

也可以直接使用全局的命名空间方式:

using namespace std;

(1)初始化

typedef struct info_s{int nNumber; }info_t; typedef std::list< info_t > list_t;

定义list的类型

list_t List; //定义一个空的链表list_t List(count); //建一个含count个默认值是0的元素的链表list_t List(count, info); //建一个含count个默认值是info的元素的链表list_t List(List2); //建一个的copy链表list_t List(List2.begin(),List2.end()); //含区间的元素[First,Last]

(2)添加(添加到末尾)

info_t info;//Set(info) List.push_back(info);

将会添加到末尾

(3)遍历

list_t::iterator iter;for(iter = List.begin(); iter != List.end() ;iter++){std::cout<< iter->nNumber <<std::endl;}

(4)删除末尾元素

c++的stl list 提供pop_back()函数来删除最后一个元素。

List.pop_back();

(5)删除所有元素

简单粗暴的方法:调用clear()

List.clear();

遍历删除法,一个一个删除,这样的好处是,如果元素有申请内容或者系统资源,我们可以把他释放了,避免资源泄漏。

list_t::iterator iter;for(iter = List.begin(); iter != List.end() ;){//这里可以做是否内存或者资源的操作//iter = List.erase(iter); //iter指向了下一个元素}

(6)插入

iter = List.insert(iter , info);

插入后iter指向新插入的元素。

(7)查找

list_t::iterator iter ;iter = std::find(List.begin(),List.end(), info);if(iter != List.end()){std::cout<<"find it"<<std::endl;}else{std::cout<<"not find it"<<std::endl;}

注意结构体需要重载==运算符

(8)排序

List.sort();

注意,结构体需要重载运算符<

上代码

#include <iostream>#include <list>#include <algorithm> #include <stdlib.h>#include <string.h> typedef struct info_s{ int nNumber; bool operator==(struct info_s b) const { return this->nNumber == b.nNumber; } bool operator!=(struct info_s b) const { return this->nNumber != b.nNumber; } bool operator>=(struct info_s b) const { return this->nNumber >= b.nNumber; } bool operator<=(struct info_s b) const { return this->nNumber <= b.nNumber; } bool operator>(struct info_s b) const { return this->nNumber > b.nNumber; } bool operator<(struct info_s b) const { return this->nNumber < b.nNumber; } }info_t; typedef std::list< info_t > list_t; void append(list_t &List, info_t &info){ std::cout<<"***append****"<<std::endl; List.push_back(info);} void for_each(list_t &List){ std::cout<<"***for_each****"<<std::endl; list_t::iterator iter; for(iter = List.begin(); iter != List.end() ;iter++) { std::cout<< iter->nNumber <<std::endl; }} void del_end_info(list_t &List){ std::cout<<"***del_end_info****"<<std::endl; if(! List.empty()) { List.pop_back(); }} void for_each_delete(list_t &List){ list_t::iterator iter; for(iter = List.begin(); iter != List.end() ;) { std::cout<< "delete before iter->number:"<<iter->nNumber <<std::endl; iter = List.erase(iter); std::cout<< "delete after iter->number:"<< iter->nNumber <<std::endl; } } int insert_one(list_t &List , info_t &info, int iPlace) { int i = 0; std::cout<<"insert_one"<<std::endl; if(iPlace < 0) { std::cout<<"insert_one param error"<<std::endl; return -1; } list_t::iterator iter = List.begin(); while(iter != List.end()) { //std::cout<<" dump "<< (*iVector)<<std::endl; if(i == iPlace) { iter = List.insert(iter , info); //此时insert的返回值是迭代器,插入成功后iVector指向插入的位置 std::cout<<" insert_one after List point "<<iter->nNumber <<std::endl; return 0; } i++; ++iter; } iter = List.insert(List.end() , info); return 0; } void find_one(list_t &List,info_t info ){ std::cout<<"find_one"<<std::endl; list_t::iterator iter ; iter = std::find(List.begin(),List.end(), info); if(iter != List.end()) { std::cout<<"find it"<<std::endl; } else { std::cout<<"not find it"<<std::endl; } } void Sort(list_t & List){ std::cout<<"Sort it"<<std::endl; List.sort(); for_each(List);} int main(){ //初始化 list_t List; info_t info; memset(&info, 0, sizeof(info_t)); //添加 info.nNumber = 8; append(List, info); info.nNumber = 5; append(List, info); info.nNumber = 7; append(List, info); info.nNumber = 1; append(List, info); info.nNumber = 1; append(List, info); info.nNumber = 2; append(List, info); info.nNumber = 1; append(List, info); //遍历 for_each(List); //插入 info.nNumber = 80; insert_one(List,info,3); for_each(List); //查找 find_one(List,info); //排序 Sort(List); //删除末尾 del_end_info(List); for_each(List); std::cout<< " size:"<<List.size()<<std::endl; //删除所有// List.clear(); for_each_delete(List); for_each(List); std::cout<< " size:"<<List.size()<<std::endl; return 0;}

以上这篇c++ STL之list对结构体的增加,删除,排序等操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章