方法一:在结构体中重载< 、>运算符,调用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
{
自定义结构体在vector中的排序实现

本文介绍了两种对vector元素为自定义结构体类型的排序方法:一是通过在结构体中重载<、>运算符,二是定义单独的比较函数。通过这两种方式,可以利用STL的sort函数对vector中的元素进行排序。示例代码展示了具体的实现过程。
最低0.47元/天 解锁文章
2696

被折叠的 条评论
为什么被折叠?



