C++ sort 对结构体进行排序
来源:互联网 发布:网络女主播经常唱的 编辑:程序博客网 时间:2024/05/30 13:42
在C++中,对自定义的结构体进行排序,需要自定义一个称为比较器的函数(下面代码中的comparison函数)
#include <iostream> #include <vector>#include <algorithm>using namespace std;struct Point { int x; int y; Point(int x, int y) : x(x), y(y) {}};//comparison函数不能写在类Solution内部//如果希望升序排序就是"<",如果希望是降序排列,就是">"bool comparison(Point a, Point b) { return a.x < b.x;}class Solution {public: vector<Point> mySort(vector<Point>& points) { sort(points.begin(), points.end(), comparison); return points; }};int main(){ while (true) { Solution solution; vector<Point> points; int count; cin >> count; for (int i = 0; i < count; i++) { int x, y; cin >> x >> y; Point p(x, y); points.push_back(p); } vector<Point> result = solution.mySort(points); for (int i = 0; i < result.size(); i++) { cout << "(" << result[i].x << ", " << result[i].y << ") "; } cout << endl; } return 0;}
0 0
- sort对结构体进行排序
- C++ sort 对结构体进行排序
- 对结构体vector进行sort(C++)
- sort对结构体进行排序(区间排序)
- 利用sort()对结构体数组进行排序
- 利用sort对结构体数组进行排序
- sort 对结构体排序
- sort对结构体排序
- 结构体排序,sort排序,c++sort
- .net 利用array.sort对结构数组进行排序
- STL中的sort对结构体排序
- qsort 与 sort 对结构体排序
- c++中sort对结构体排序
- 利用sort对结构体数组排序
- 利用sort对结构体排序
- C语言-对一个结构体中的字段进行排序
- 【C++】:用sort对string类型进行排序
- ACM零起点2017-7-25(sort对结构体排序 PK 自创C语言对结构体快排)
- activity-生命周期
- Markdown 语法学习
- Bootstrap学习笔记之插件(三)
- Spring3 集成 Hibernate4 配置
- 写一个方法对日期格式化 new Date().format("yyyy-MM-dd HH:mm:ss")
- C++ sort 对结构体进行排序
- python爬虫,selenium使用,Firefox-chrome-IE问题解决方法,selenium的基本操作
- 【转】 Java类加载全过程
- python随笔1
- jQuery 事件
- 牛人主页(主页有很多论文代码)【真的好强大】
- 如何处理OCR文字识别软件中出现错误代码142和55的问题
- SQLite3查询表结构
- Gif动画播放一次就停止