如何利用C++/STL vector的sort算法对元素是自定义类型进行排序?

frcsun 2010-06-20 08:00:17
STL中提供了高效的排序算法,vector中是sort函数。当vector的元素数据类型是基本数据类型(int,double)时自然可以调用sort进行排序。但是当vector的元素是我们自定义的类或结构类型呢?如定义class Student{ public string name;public double score;}定义vector<Student> stuVector;想对stuVector按照score成绩进行排序该如何做呢?无法利用STL提供的算法,必须自己手动实现排序吗?

在C#中为Student实现IEnumerable借口就能调用类库算法了,C++中有这样的功能么?

说的有点啰嗦,但是相信大家还是能够理解的,在此先谢过了~~~
...全文
10086 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
suker21 2012-02-13
  • 打赏
  • 举报
回复
应该看卡这篇
http://topic.csdn.net/t/20050907/22/4256119.html
ccivi 2011-06-23
  • 打赏
  • 举报
回复
高~~~
diudiushare 2011-05-22
  • 打赏
  • 举报
回复
受教了,谢谢啦
liuy0202 2010-06-24
  • 打赏
  • 举报
回复
学习了,很不错~~
blackboycpp 2010-06-21
  • 打赏
  • 举报
回复
直接sort就完了呗, 元素的类型要支持<运算符
name61 2010-06-21
  • 打赏
  • 举报
回复
其实我很喜欢加菲狗
philipzeng 2010-06-21
  • 打赏
  • 举报
回复
自定义一个比较函数即可,
frcsun 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fallening 的回复:]

引用楼主 frcsun 的回复:
STL中提供了高效的排序算法,vector中是sort函数。当vector的元素数据类型是基本数据类型(int,double)时自然可以调用sort进行排序。但是当vector的元素是我们自定义的类或结构类型呢?如定义class Student{ public string name;public double score;}定义vector<Student>……
[/Quote]

这个是调用boost库里的函数么?
cs_yagami 2010-06-21
  • 打赏
  • 举报
回复
算是有三個方法,寫個cmp函數,寫個cmp仿函數,重載<,其實就是2樓所寫的那樣。
仿函數不懂去查一下吧~
基本上直接重載<就足矣~
ForestDB 2010-06-21
  • 打赏
  • 举报
回复
函数对象或者重载<运算符。
tjyjx7946358 2010-06-20
  • 打赏
  • 举报
回复
呵呵。这个我知道。查过sort的用法。第3个参数重载<,自定义比较规则。。
疯狂奶豆腐 2010-06-20
  • 打赏
  • 举报
回复
受教了啊
apoorcowboy 2010-06-20
  • 打赏
  • 举报
回复
自定义一个比较函数,这个函数是对<的重载
sort的第三个参数为上面自定义函数的实体
比如这个函数是cmp,哪么第三个参数就是cmp()
fallening 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 frcsun 的回复:]
STL中提供了高效的排序算法,vector中是sort函数。当vector的元素数据类型是基本数据类型(int,double)时自然可以调用sort进行排序。但是当vector的元素是我们自定义的类或结构类型呢?如定义class Student{ public string name;public double score;}定义vector<Student> stuVector;想对stuVe……
[/Quote]


#include <boost/lambda/lambda.hpp>
class Student{ public string name;public double score;}
vector<Student> stuVector;
sort( stuVector.begin(), stuVector.end(), _1.score > _2.score );

cattycat 2010-06-20
  • 打赏
  • 举报
回复
sort的第三个参数是你自己定义的函数对象,其实就是struct 的结构体,重载一下operator()即可。
pengzhixi 2010-06-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 frcsun 的回复:]
STL中提供了高效的排序算法,vector中是sort函数。当vector的元素数据类型是基本数据类型(int,double)时自然可以调用sort进行排序。但是当vector的元素是我们自定义的类或结构类型呢?如定义class Student{ public string name;public double score;}定义vector<Student> stuVector;想对stuVec……
[/Quote]

sort是有两个版本的
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
版本2你可以提供元素的比较规则
因为版本1默认元素可以按严格的弱序比较
liutengfeigo 2010-06-20
  • 打赏
  • 举报
回复
哟系。。。学习了,正在看VECTOR
太乙 2010-06-20
  • 打赏
  • 举报
回复
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;


在你的结构体理,加上这么一个函数~~~
npuhuxl 2010-06-20
  • 打赏
  • 举报
回复
定义排序函数:
bool Less(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //可以自己设定
}

std::sort(sutVector.begin(), stuVector.end(), Less);

或者
bool operator<(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //可以自己设定
}
std::sort(sutVector.begin(), stuVector.end());

或者
struct Less
{
bool operator()(const Student& s1, const Student& s2)
{
return s1.name < s2.name; //可以自己设定
}
};

std::sort(sutVector.begin(), stuVector.end(), Less());

//或重载student的operato<
太乙 2010-06-20
  • 打赏
  • 举报
回复
// sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;

// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)

// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}
加载更多回复(1)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧