vector元素为自定义数据时如何进行排序
2013-03-14 11:43
471 查看
方法一:在结构体中重载< 、>运算符,调用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;
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/61c17ed2eb67211a84e137429ac3f7ad.gif)
方法二:
单独定义比较函数,调用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;
}
#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;
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/61c17ed2eb67211a84e137429ac3f7ad.gif)
方法二:
单独定义比较函数,调用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;
}
相关文章推荐
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- Excel学习笔记002-002:工作表内及工作表间、工作簿间单元格数据的复制、剪切、粘贴;如何进行成绩排序。
- 对自定义对象集合List中的数据进行排序
- 如何对NSArray中自定义的对象进行排序
- 在S2SH整合中如何对其多表联合查询出来的数据,进行分页和排序
- java 如何对自定义的对象进行排序
- 「 Hadoop」mapreduce对温度数据进行自定义排序、分组、分区等
- Excel学习笔记002-002:工作表内及工作表间、工作簿间单元格数据的复制、剪切、粘贴;如何进行成绩排序。
- java 如何对自定义的对象进行排序
- 如何在“元素为自定义类型的vector”中查找指定的元素?
- Excel中如何对数据进行简单排序
- 编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
- 如何利用外部列表元素顺序对源列表进行排序
- Java 中List数据,自定义对象按指定的属性进行排序
- 获取DataGridView绑定BindingSource的数据表,并根据树控件点击进行筛选数据表,下拉控件自定义排序问题初步解决方法,解决了该行属于另一个表的错误提示.
- 如何使用 DataView 进行数据排序和检索
- java如何对List集合中的元素进行排序(请收藏)
- FLEAPHP 如何对关联表数据进行排序?