STL中sort函数用法
来源:互联网 发布:js对象转数组 编辑:程序博客网 时间:2024/06/01 10:28
sort()头文件是:#include <algorithm>
sort()函数用法有很多种,重载运算符,定义比较函数。
我们重载>和<运算符就能够运用less,greater,less_equal进行比较排序。
也可以定义比较函数。
Point.h
#ifndef POINT_H#define POINT_Hclass Point{ private: double x; double y; public: Point(); Point(double x1, double y1); void Setter(double x1, double y1); bool operator>(const Point& b)const; bool operator<(const Point& b)const; void show()const; virtual ~Point(); protected:};#endif // POINT_H
#include "Point.h"#include <iostream>using namespace std;Point::Point(){ //ctor}Point::Point(double x1, double y1){ x = x1, y = y1;}bool Point::operator>(const Point& b) const{ return x > b.x || y > b.y;}bool Point::operator<(const Point& b) const{ return x < b.x || y < b.y;}void Point::Setter(double x1, double y1){ x = x1, y = y1;}void Point::show()const{ cout << x <<" "<< y << endl;}Point::~Point(){ //dtor}
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include "Point.h"using namespace std;bool cmp_less(const Point& a, const Point& b){ //升序排列 return a.x < b.x || a.y < b.y;}bool cmp_greater(const Point& a, const Point& b){//降序排列 return a.x > b.x || a.y > b.y;}int main(){ Point a[10]; for (int i = 0; i < 10; i ++) { a[i].Setter(i, i); } sort(a, a+10,less<Point>());//升序排列 for (int i = 0; i < 10; i ++) { a[i].show(); } cout <<endl; sort(a, a+10,greater<Point>());//降序排列 for (int i = 0; i < 10; i ++) { a[i].show(); } return 0;}
0 0
- STL中sort函数用法
- STL 中 sort 函数用法简介
- STL 中 sort 函数用法简介
- STL中sort函数的用法
- STL 中 sort 函数用法简介
- STL 中 sort 函数用法简介
- 1.STL 中 sort 函数用法简介
- STL中sort()函数的用法详解
- STL 中 sort 函数用法简介
- STL中sort()函数的用法
- STL 中 sort函数用法简介
- STL中list容器中sort函数用法
- 【STL】sort函数的用法
- STL sort函数的用法
- STL sort函数的用法
- C++:STL中sort()函数的用法总结
- STL 中 sort()函数用法详解—— 排序
- stl中sort函数详解
- 家居破财的风水布局
- package导致错误
- do...while(0)的妙用
- 顺序编程
- 二、Linux下给eclipse配置tomcat
- STL中sort函数用法
- C++对象的浅复制和深复制
- 链表的冒泡排序。
- spring mvc 设配器模式应用
- phpstorm 显示行号
- UITableViewCell autolayout Unable to simultaneously satisfy constraints.
- ruby 2.x.x之新特性散谈
- bash参考手册之六(Bash特性)续四
- 度分 格式 转换 GPS 经纬度定义、经纬度格式、GDAL中地理坐标转换及地理坐标屏幕显示