您的位置:首页 > 编程语言 > C语言/C++

C++中,结构体vector使用sort排序(以及sort参数错误问题)

2017-03-01 16:19 399 查看
转载自:http://blog.csdn.net/zhouxun623/article/details/49887555

转载自:http://bbs.csdn.net/topics/391030749

转载一:

一、遇到问题:

今天写代码的是遇到想对vector进行排序的问题,隐约记得std::sort函数是可以对vector进行排序的,但是这次需要排序的vector中压的是自己定义的结构体(元素大于等于2),想以其中某一个元素进行正序或逆序排序,则不能直接使用sort函数。

二、解决方案:

1.C++中当 vector 中的数据类型为基本类型时,我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,代码如下(摘自http://www.cplusplus.com/reference/algorithm/sort/):

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

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};
std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

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

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

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

// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}


输出为:

myvector contains: 12 26 32 33 45 53 71 80


2.然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现排序?

其实就是对上面代码中std::sort函数的第三个参数comp调用的函数或object进行修改即可。在这里我们使用函数作为comp作为例子,代码如下:

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

struct Point2
{
int x;
int y;
};

bool GreaterSort (Point2 a,Point2 b) { return (a.x>b.x); }
bool LessSort (Point2 a,Point2 b) { return (a.x<b.x); }

int main()
{
vector<Point2> aaa;
Point2 temp;

temp.x=1;
temp.y=1;
aaa.push_back(temp);

temp.x=2;
temp.y=2;
aaa.push_back(temp);

temp.x=3;
temp.y=3;
aaa.push_back(temp);

sort(aaa.begin(),aaa.end(),GreaterSort);//降序排列
cout<<"Greater Sort:"<<endl;
for (int i =0;i<aaa.size();i++)
{
cout<<aaa[i].x<<"   "<<aaa[i].y<<endl;
}

sort(aaa.begin(),aaa.end(),LessSort);//升序排列
cout<<"Less Sort:"<<endl;
for (int i =0;i<aaa.size();i++)
{
cout<<aaa[i].x<<"   "<<aaa[i].y<<endl;
}

return 1;
}


运行结果如下:

Greater Sort:
3       3
2       2
1       1
Less Sort:
1       1
2       2
3       3


以上代码在visual stdio 2012环境下编译通过,也是自己在实践过程中的总结,如有不妥的地方,欢迎您指出。

三、参考文献:

http://www.cplusplus.com/reference/algorithm/sort/

http://blog.csdn.net/aguisy/article/details/5787257

转载二:

问题:sort函数出错“应输入 2 个参数,却提供了 3 个”?:

我想写一个类的成员函数用于做排序,sort函数的第三个参数compare_degree()是一个比较函数,我想把他封装到类成员函数里面,但是程序报错如下:

错误 1 error C3867: “matrix::compare_degree”: 函数调用缺少参数列表;请使用“&matrix::compare_degree”创建指向成员的指针 f:\work\project1\project1\test.cpp 45 1 Project1

错误 2 error C2780: “void std::sort(_RanIt,_RanIt)”: 应输入 2 个参数,却提供了 3 个f:\work\project1\project1\test.cpp 45 1 Project1

于是,我尝试把这个比较函数写在类的外面,重写了一个compare()的比较函数在却可以编译通过,请问各位大侠这是为什么呢?

#include <vector>
#include <algorithm>

using namespace std;

struct node
{
int index;//节点序号
int degree;//节点度
};

class matrix
{
public:
matrix();
~matrix();
vector<node> sort_degree();
bool compare_degree(node node_a, node node_b);

private:
vector<node> nodes;
};

bool compare(node node_a, node node_b);

matrix::matrix()
{
}

matrix::~matrix()
{
}

bool matrix::compare_degree(node node_a, node node_b)
{
//按照节点度降序排列
return (node_a.degree > node_b.degree);
}

bool compare(node node_a, node node_b)
{
//按照节点度降序排列
return (node_a.degree > node_b.degree);
}

vector<node> matrix::sort_degree()
{
vector<node> temp_nodes = nodes;
//根据compare_degree规定的规则对数组内的节点排序
sort(temp_nodes.begin(), temp_nodes.end(), compare_degree);
sort(temp_nodes.begin(), temp_nodes.end(), compare);
return temp_nodes;
}

int main(void)
{
matrix G;
vector<node> G_degree_rank;

G_degree_rank = G.sort_degree();
}


回复:

要想取类成员函数,作为参数,该函数必须是static的。你把compare 声明加上static试试看

回复:

compare_degree compare

成员函数第一个参数默认是this指针, 所以这两个函数其实是3个参数

加static 静态函数就没有this指针

回复:

是的,17行改为如下就可以了:

static bool compare_degree(node node_a, node node_b);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++
相关文章推荐