C++中,结构体vector使用sort排序(以及sort参数错误问题)
2017-03-01 16:19
399 查看
转载自:http://blog.csdn.net/zhouxun623/article/details/49887555
转载自:http://bbs.csdn.net/topics/391030749
输出为:
2.然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现排序?
其实就是对上面代码中std::sort函数的第三个参数comp调用的函数或object进行修改即可。在这里我们使用函数作为comp作为例子,代码如下:
运行结果如下:
以上代码在visual stdio 2012环境下编译通过,也是自己在实践过程中的总结,如有不妥的地方,欢迎您指出。
http://blog.csdn.net/aguisy/article/details/5787257
错误 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()的比较函数在却可以编译通过,请问各位大侠这是为什么呢?
成员函数第一个参数默认是this指针, 所以这两个函数其实是3个参数
加static 静态函数就没有this指针
static bool compare_degree(node node_a, node node_b);
转载自: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++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数出错“应输入 2 个参数,却提供了 3 个)
- c++------用结构体以及栈的使用来解决迷宫问题
- C++中,结构体vector使用sort排序
- C++ 结构体vector使用sort排序
- 实战c++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数“应输入 2 个参数,却提供了 3 个)
- (2011.07.06)C++ 结构体中字符指针在main中使用new的赋值问题。
- VS2012 使用MySql的API函数连接数据库(成功), 以及使用mysql-connector-c++-1.1.3遇到的问题!
- WinForm中DataGridView使用DataView的Sort排序后删除数据行数错误问题
- 使用debmirror为Debian建立本地源的方法,以及遇到的GPG key验证错误问题的解决方法
- C++中使用pair和make_pair在vector, sort -- ZZ
- C/C++语言宏中使用参数做结构体成员
- 使用预声明解决结构体互相引用以及头文件内容互相包含的问题
- C++使用sort对于结构体排序
- WCF Rest:不使用UriTemplate使用post方式传参解决HTTP400问题以及参数映射问题
- C++中结构体、联合、枚举的使用以及函数相关
- 使用预声明解决结构体互相引用以及头文件内容互相包含的问题[转]
- (2011.09.17)C/C++中二进制文件的使用以及字节序问题的图示
- c++ 的sort模板使用以及运算符重载
- C/C++编写dll动态链接库使用操作与非基本类型参数声明错误
- C/C++编写dll动态链接库使用操作与非基本类型参数声明错误(2)