vector元素为自定义结构体类型时如何对容器元素进行排序?
来源:互联网 发布:mac鼠标能点右键吗 编辑:程序博客网 时间:2024/05/16 12:50
方法一:在结构体中重载< 、>运算符,调用STL的sort()函数
#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/tigernana/article/details/7293758
0 0
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- vector元素为自定义数据时如何进行排序
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- C++ 根据vector 容器内存放的自定义结构的某个元素 进行升序降序排序
- 对vector 中的结构体元素排序
- 对容器元素进行排序
- STL中vector容器中元素为自定义类型时的效率分析
- STL中vector容器中元素为自定义类型指针时的内存泄漏问题
- 简谈俩种不同方式对vector容器的 自定义结构体进行排序
- STL中对vector容器中的结构体进行排序
- 如何在“元素为自定义类型的vector”中查找指定的元素?
- 对ArrayList的自定义元素进行排序
- C++对结构体按照某一项元素进行排序
- 元素为自定义复合结构时 map,set 如何处理重复 key 及排序?
- 如何对HashMap中的元素进行排序
- 元素为结构体的vector 找最大元素
- 元素为结构体的vector 找最大元素
- 不用加号求加法运算
- 栈的顺序存储和链式存储
- ortp库使用入门
- #include<bitset>
- hdu1018阶乘位数
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- Oracle systemstatedump 会加锁么?
- 关于时间函数的一些有用的SQL脚本
- 如何将Android Studio项目提交(更新)到github
- 5.17学习内容 android判断软键盘状态、系统键盘监控、生成中间logo的二维码
- stk6.1安装方法
- poj 1651 区间dp
- 1029. 旧键盘(20)
- poj 2446 二分图最大匹配 匈牙利算法