vector 结构体类型 使用 排序
来源:互联网 发布:c语言函数写法 编辑:程序博客网 时间:2024/05/12 05:49
如果要在Vector容器中存放结构体类型的变量,经常见到两种存放方式.
方式一:放入这个结构体类型变量的副本。方式二:放入指向这个结构体类型变量的指针。
假设结构体类型变量是这样的,
typedef struct student{ char school_name[100]; char gender; int age; bool is_absent;} StudentInfo;
那么,方式一和方式二的实现分别如下所示:
/*[方式一] 结构体放栈中,vector中放副本---------------------*/#include <iostream>#include <string>#include <vector>typedef struct student{ char school_name[100]; char gender; int age; bool is_absent;} StudentInfo; typedefstd::vector<StudentInfo> StudentInfoVec;void print(StudentInfoVec* stduentinfovec){ for (int j=0;j<(*stduentinfovec).size();j++) { std::cout<< (*stduentinfovec)[j].school_name<<"\t"<< (*stduentinfovec)[j].gender<<"\t"<< (*stduentinfovec)[j].age<<"\t"<< (*stduentinfovec)[j].is_absent<<"\t"<<std::endl; } return;} int main(){ StudentInfo micheal={"Micheal",'m',18,false}; StudentInfo cherry={"Cherry",'f',16,true}; StudentInfoVec studentinfovec; studentinfovec.push_back(micheal); studentinfovec.push_back(cherry); print(&studentinfovec); return 0;}
方式一的输出结果
/*[方式二] 结构体放入堆中,vector中放指针---------------------*/typedef struct student{ char* school_name; char gender; int age; bool is_absent;} StudentInfo; typedefstd::vector<StudentInfo*> StudentInfoPtrVec;void print(StudentInfoPtrVec*stduentinfoptrvec){ for (int j=0;j<(*stduentinfoptrvec).size();j++) { std::cout<< (*stduentinfoptrvec)[j]->school_name<<"\t"<< (*stduentinfoptrvec)[j]->gender<<"\t"<< (*stduentinfoptrvec)[j]->age<<"\t"<< (*stduentinfoptrvec)[j]->is_absent<<"\t"<<std::endl; } return;}int main(){ StudentInfoPtrVec studentinfoptrvec; char* p_char_1=NULL; p_char_1=new char[100]; strcpy(p_char_1,"Micheal"); StudentInfo* p_student_1=new StudentInfo; p_student_1->school_name=p_char_1; p_student_1->gender='m'; p_student_1->age=18; p_student_1->is_absent=false; studentinfoptrvec.push_back(p_student_1); char* p_char_2=NULL; p_char_2=new char[100]; strcpy(p_char_2,"Cherry"); StudentInfo* p_student_2=new StudentInfo; p_student_2->school_name=p_char_2; p_student_2->gender='f'; p_student_2->age=16; p_student_2->is_absent=false; studentinfoptrvec.push_back(p_student_2); print(&studentinfoptrvec); delete p_char_1; delete p_student_1; delete p_char_2; delete p_student_2; return 0; }
方式二的输出结果,同上,依然是
类 结构体 使用实例
#include "stdafx.h"#include <vector>#include <string>using namespace std;class AClass{public: int num; string name;};struct AStruct { int num; string name;};void TestStruct(){ //类的使用 AClass Ac; vector<AClass> vc; Ac.num=10; Ac.name="name"; vc.push_back(Ac); AClass d; for (vector<AClass>::iterator it=vc.begin();it<vc.end();++it) { d=*it; cout<<d.num<<endl; } //结构体的使用 AStruct As; vector<AStruct> vs; As.num=10; As.name="name"; vs.push_back(As); AStruct ds; for (vector<AStruct>::iterator it=vs.begin();it<vs.end();++it) { ds=*it; cout<<ds.num<<endl; } }void TestPoint(){ //类的使用 AClass *Ac=new AClass; vector<AClass *> vc; Ac->num=10; Ac->name="name"; vc.push_back(Ac); AClass *d; for (vector<AClass*>::iterator it=vc.begin();it<vc.end();++it) { d=*it; cout<<d->num<<endl; } }int _tmain(int argc, _TCHAR* argv[]){ TestStruct(); TestPoint(); int n; cin>>n; return 0;}
排序:
方法一:在结构体中重载< 、>运算符,调用STL的sort()函数#include "stdafx.h"#include <vector>#include <algorithm>#include <iostream>using namespace std;class MYSTRUCT{public: int id; int nums; vector<int> vec; MYSTRUCT() { id=numeric_limits<int>::max(); nums=0; vec.resize(0); }//重载== bool operator==( const MYSTRUCT& objstruct) const { return objstruct.id==id; }//重载< bool operator<(const MYSTRUCT& objstruct) const { return id<objstruct.id; }//重载> bool operator>(const MYSTRUCT& objstuct) const { return id>objstuct.id; }};int _tmain(int argc, _TCHAR* argv[]){ vector<MYSTRUCT> structs; for(int i=0;i<9;i++) { MYSTRUCT myStruct; //myStruct.id=i; myStruct.nums=i; structs.push_back(myStruct); } structs[0].id=9; structs[1].id=1; structs[2].id=7; structs[3].id=3; structs[4].id=8; structs[5].id=2; structs[6].id=6; structs[7].id=0; structs[8].id=10; sort(structs.begin(),structs.end()); for(vector<MYSTRUCT>::iterator it=structs.begin();it!=structs.end();++it) { std::cout<<it->id<<endl; } return 0;}微笑方法二: 单独定义比较函数,调用STL的sort()函数,不修改结构体#include "stdafx.h"#include <vector>#include <algorithm>#include <iostream>using namespace std;class MYSTRUCT{public: int id; int nums; vector<int> vec; MYSTRUCT() { id=numeric_limits<int>::max(); nums=0; vec.resize(0); }// //重载==// bool operator==( const MYSTRUCT& objstruct) const// {// return objstruct.id==id;// }// // //重载<// bool operator<(const MYSTRUCT& objstruct) const// {// return id<objstruct.id;// }// // //重载>// bool operator>(const MYSTRUCT& objstuct) const// {// return id>objstuct.id;// }}; bool lessCompare(const MYSTRUCT& obj1,const MYSTRUCT& obj2) { return obj1.id<obj2.id; } bool greaterCompare(const MYSTRUCT& obj1,const MYSTRUCT& obj2) { return obj1.id>obj2.id; } int _tmain(int argc, _TCHAR* argv[]) { vector<MYSTRUCT> structs; for(int i=0;i<9;i++) { MYSTRUCT myStruct; //myStruct.id=i; myStruct.nums=i; structs.push_back(myStruct); } structs[0].id=9; structs[1].id=1; structs[2].id=7; structs[3].id=3; structs[4].id=8; structs[5].id=2; structs[6].id=6; structs[7].id=0; structs[8].id=10; sort(structs.begin(),structs.end(),lessCompare); for(vector<MYSTRUCT>::iterator it=structs.begin();it!=structs.end();++it) { std::cout<<it->id<<endl; } return 0;}
参考转自:
http://blog.csdn.net/feliciafay/article/details/9128385
http://blog.csdn.net/loveheronly/article/details/7900799
http://blog.csdn.net/tigernana/article/details/7293758
0 0
- vector 结构体类型 使用 排序
- vector的使用,结构体排序
- vector的使用,结构体排序
- C++ 结构体vector使用sort排序
- 自定义结构体vector排序
- C++中,结构体vector使用sort排序
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- vector容器结构体类型查找
- 结构体、类使用vector
- vector 结构体排序典型应用
- vector中自定义结构体的排序
- c++用vector对结构体排序
- vector sort结构体排序要点
- 自定义结构体vector的排序
- 对vector 中的结构体元素排序
- 1025 排序(vector存结构体)
- C++中,结构体vector使用sort排序(以及sort参数错误问题)
- 2015.08.29,今天开通CSDN博客了!
- Joison博客路之开启篇
- Oracle(四)--> 高级子查询(多例详解难度高,实在不信进来看~)
- Socket编程(1)
- RPG黑暗之光(6)状态栏/技能系统/武器系统/小地图/头像栏
- vector 结构体类型 使用 排序
- MySQL数据类型和常用字段属性总结
- 修改系统tabbar的背景色
- 欢迎使用CSDN-markdown编辑器
- LeetCode(241)Different Ways to Add Parentheses
- maven中net.sf.json报错的解决方法
- 在使用jetty配置jndi的时候报错“Need to specify class name in environment or system property, or as an applet pa
- Linux nc命令详解
- 配置mac自带Apache和php