前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sort函数对vector排序_sort函数对结构体数组排序

sort函数对vector排序_sort函数对结构体数组排序

作者头像
全栈程序员站长
发布2022-09-21 15:07:43
1.4K0
发布2022-09-21 15:07:43
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

一、遇到问题:

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

二、解决方案:

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

代码语言:javascript
复制
// 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;
}

输出为:

代码语言:javascript
复制
myvector contains: 12 26 32 33 45 53 71 80

2.然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现排序? 其实就是对上面代码中std::sort函数的第三个参数comp调用的函数或object进行修改即可。在这里我们使用函数作为comp作为例子,代码如下:

代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
运行结果如下:
代码语言:javascript
复制
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

(转载请注明作者和出处:http://blog.csdn.net/zhouxun 未经允许请勿用于商业用途)

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169876.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、遇到问题:
  • 二、解决方案:
  • 三、参考文献:
    • (转载请注明作者和出处:http://blog.csdn.net/zhouxun 未经允许请勿用于商业用途)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档