当前位置:   article > 正文

vector 结构体类型 使用 排序

vector 结构体类型 使用 排序

如果要在Vector容器中存放结构体类型的变量,经常见到两种存放方式.

方式一:放入这个结构体类型变量的副本。
方式二:放入指向这个结构体类型变量的指针。
假设结构体类型变量是这样的,
  1. typedef struct student{
  2. char school_name[100];
  3. char gender;
  4. int age;
  5. bool is_absent;
  6. } StudentInfo;

那么,方式一和方式二的实现分别如下所示:
  1. /*[方式一] 结构体放栈中,vector中放副本---------------------*/
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. typedef struct student{
  6. char school_name[100];
  7. char gender;
  8. int age;
  9. bool is_absent;
  10. } StudentInfo;
  11. typedefstd::vector<StudentInfo> StudentInfoVec;
  12. void print(StudentInfoVec* stduentinfovec){
  13. for (int j=0;j<(*stduentinfovec).size();j++)
  14. {
  15. std::cout<<
  16. (*stduentinfovec)[j].school_name<<"\t"<<
  17. (*stduentinfovec)[j].gender<<"\t"<<
  18. (*stduentinfovec)[j].age<<"\t"<<
  19. (*stduentinfovec)[j].is_absent<<"\t"<<std::endl;
  20. }
  21. return;
  22. }
  23. int main(){
  24. StudentInfo micheal={"Micheal",'m',18,false};
  25. StudentInfo cherry={"Cherry",'f',16,true};
  26. StudentInfoVec studentinfovec;
  27. studentinfovec.push_back(micheal);
  28. studentinfovec.push_back(cherry);
  29. print(&studentinfovec);
  30. return 0;
  31. }

方式一的输出结果

  1. /*[方式二] 结构体放入堆中,vector中放指针---------------------*/
  2. typedef struct student{
  3. char* school_name;
  4. char gender;
  5. int age;
  6. bool is_absent;
  7. } StudentInfo;
  8. typedefstd::vector<StudentInfo*> StudentInfoPtrVec;
  9. void print(StudentInfoPtrVec*stduentinfoptrvec){
  10. for (int j=0;j<(*stduentinfoptrvec).size();j++)
  11. {
  12. std::cout<<
  13. (*stduentinfoptrvec)[j]->school_name<<"\t"<<
  14. (*stduentinfoptrvec)[j]->gender<<"\t"<<
  15. (*stduentinfoptrvec)[j]->age<<"\t"<<
  16. (*stduentinfoptrvec)[j]->is_absent<<"\t"<<std::endl;
  17. }
  18. return;
  19. }
  20. int main(){
  21. StudentInfoPtrVec studentinfoptrvec;
  22. char* p_char_1=NULL;
  23. p_char_1=new char[100];
  24. strcpy(p_char_1,"Micheal");
  25. StudentInfo* p_student_1=new StudentInfo;
  26. p_student_1->school_name=p_char_1;
  27. p_student_1->gender='m';
  28. p_student_1->age=18;
  29. p_student_1->is_absent=false;
  30. studentinfoptrvec.push_back(p_student_1);
  31. char* p_char_2=NULL;
  32. p_char_2=new char[100];
  33. strcpy(p_char_2,"Cherry");
  34. StudentInfo* p_student_2=new StudentInfo;
  35. p_student_2->school_name=p_char_2;
  36. p_student_2->gender='f';
  37. p_student_2->age=16;
  38. p_student_2->is_absent=false;
  39. studentinfoptrvec.push_back(p_student_2);
  40. print(&studentinfoptrvec);
  41. delete p_char_1;
  42. delete p_student_1;
  43. delete p_char_2;
  44. delete p_student_2;
  45. return 0;
  46. }

方式二的输出结果,同上,依然是


类 结构体 使用实例

  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. class AClass
  6. {
  7. public:
  8. int num;
  9. string name;
  10. };
  11. struct AStruct
  12. {
  13. int num;
  14. string name;
  15. };
  16. void TestStruct()
  17. {
  18. //类的使用
  19. AClass Ac;
  20. vector<AClass> vc;
  21. Ac.num=10;
  22. Ac.name="name";
  23. vc.push_back(Ac);
  24. AClass d;
  25. for (vector<AClass>::iterator it=vc.begin();it<vc.end();++it)
  26. {
  27. d=*it;
  28. cout<<d.num<<endl;
  29. }
  30. //结构体的使用
  31. AStruct As;
  32. vector<AStruct> vs;
  33. As.num=10;
  34. As.name="name";
  35. vs.push_back(As);
  36. AStruct ds;
  37. for (vector<AStruct>::iterator it=vs.begin();it<vs.end();++it)
  38. {
  39. ds=*it;
  40. cout<<ds.num<<endl;
  41. }
  42. }
  43. void TestPoint()
  44. {
  45. //类的使用
  46. AClass *Ac=new AClass;
  47. vector<AClass *> vc;
  48. Ac->num=10;
  49. Ac->name="name";
  50. vc.push_back(Ac);
  51. AClass *d;
  52. for (vector<AClass*>::iterator it=vc.begin();it<vc.end();++it)
  53. {
  54. d=*it;
  55. cout<<d->num<<endl;
  56. }
  57. }
  58. int _tmain(int argc, _TCHAR* argv[])
  59. {
  60. TestStruct();
  61. TestPoint();
  62. int n;
  63. cin>>n;
  64. return 0;
  65. }


排序:

  1. 方法一:在结构体中重载< 、>运算符,调用STL的sort()函数
  2. #include "stdafx.h"
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6. using namespace std;
  7. class MYSTRUCT
  8. {
  9. public:
  10. int id;
  11. int nums;
  12. vector<int> vec;
  13. MYSTRUCT()
  14. {
  15. id=numeric_limits<int>::max();
  16. nums=0;
  17. vec.resize(0);
  18. }
  19. //重载==
  20. bool operator==( const MYSTRUCT& objstruct) const
  21. {
  22. return objstruct.id==id;
  23. }
  24. //重载<
  25. bool operator<(const MYSTRUCT& objstruct) const
  26. {
  27. return id<objstruct.id;
  28. }
  29. //重载>
  30. bool operator>(const MYSTRUCT& objstuct) const
  31. {
  32. return id>objstuct.id;
  33. }
  34. };
  35. int _tmain(int argc, _TCHAR* argv[])
  36. {
  37. vector<MYSTRUCT> structs;
  38. for(int i=0;i<9;i++)
  39. {
  40. MYSTRUCT myStruct;
  41. //myStruct.id=i;
  42. myStruct.nums=i;
  43. structs.push_back(myStruct);
  44. }
  45. structs[0].id=9;
  46. structs[1].id=1;
  47. structs[2].id=7;
  48. structs[3].id=3;
  49. structs[4].id=8;
  50. structs[5].id=2;
  51. structs[6].id=6;
  52. structs[7].id=0;
  53. structs[8].id=10;
  54. sort(structs.begin(),structs.end());
  55. for(vector<MYSTRUCT>::iterator it=structs.begin();it!=structs.end();++it)
  56. {
  57. std::cout<<it->id<<endl;
  58. }
  59. return 0;
  60. }
  61. 微笑方法二: 单独定义比较函数,调用STL的sort()函数,不修改结构体
  62. #include "stdafx.h"
  63. #include <vector>
  64. #include <algorithm>
  65. #include <iostream>
  66. using namespace std;
  67. class MYSTRUCT
  68. {
  69. public:
  70. int id;
  71. int nums;
  72. vector<int> vec;
  73. MYSTRUCT()
  74. {
  75. id=numeric_limits<int>::max();
  76. nums=0;
  77. vec.resize(0);
  78. }
  79. // //重载==
  80. // bool operator==( const MYSTRUCT& objstruct) const
  81. // {
  82. // return objstruct.id==id;
  83. // }
  84. //
  85. // //重载<
  86. // bool operator<(const MYSTRUCT& objstruct) const
  87. // {
  88. // return id<objstruct.id;
  89. // }
  90. //
  91. // //重载>
  92. // bool operator>(const MYSTRUCT& objstuct) const
  93. // {
  94. // return id>objstuct.id;
  95. // }
  96. };
  97. bool lessCompare(const MYSTRUCT& obj1,const MYSTRUCT& obj2)
  98. {
  99. return obj1.id<obj2.id;
  100. }
  101. bool greaterCompare(const MYSTRUCT& obj1,const MYSTRUCT& obj2)
  102. {
  103. return obj1.id>obj2.id;
  104. }
  105. int _tmain(int argc, _TCHAR* argv[])
  106. {
  107. vector<MYSTRUCT> structs;
  108. for(int i=0;i<9;i++)
  109. {
  110. MYSTRUCT myStruct;
  111. //myStruct.id=i;
  112. myStruct.nums=i;
  113. structs.push_back(myStruct);
  114. }
  115. structs[0].id=9;
  116. structs[1].id=1;
  117. structs[2].id=7;
  118. structs[3].id=3;
  119. structs[4].id=8;
  120. structs[5].id=2;
  121. structs[6].id=6;
  122. structs[7].id=0;
  123. structs[8].id=10;
  124. sort(structs.begin(),structs.end(),lessCompare);
  125. for(vector<MYSTRUCT>::iterator it=structs.begin();it!=structs.end();++it)
  126. {
  127. std::cout<<it->id<<endl;
  128. }
  129. return 0;
  130. }


参考转自:

http://blog.csdn.net/feliciafay/article/details/9128385

http://blog.csdn.net/loveheronly/article/details/7900799

http://blog.csdn.net/tigernana/article/details/7293758

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/243927
推荐阅读
相关标签
  

闽ICP备14008679号