数组结构体中排序
2016-03-17 10:37
281 查看
//sort排序用法
//函数原型: void sort(iterator begin, iterator end);
//将[begin,end) 之间的元素使用默认的<进行排序
#include <algorithm> // 包含头文件
//自定义排序函数
bool cmp(const int a,const int b)
{ return a > b;}
int main()
{
int a[5]={3,4,1,2,5};
sort(a,a+5,cmp); //cmp,降序排序 5,4,3,2,1
sort(a,a+5); //默认升序排序,1,2,3,4,5
return 0;
}
//sort,自定义数据结构
#include <iostream>
#include <algorithm>
using namespace std;
struct Node
{
int x;
int y;
bool operator < (const struct Node& a) //重载<号
{
return x < a.x; //按x升序
//return x >a.x; //按x降序
}
}node[10];
int main()
{
node[0].x = 5;
node[1].x = 3;
node[2].x = 4;
node[3].x = 1;
node[4].x = 2;
sort(node,node+5);
for(int i = 0; i < 5; i++)
{
cout<<node[i].x<<endl; // 1 2 3 4 5
}
return 0;
}
//函数原型: void sort(iterator begin, iterator end);
//将[begin,end) 之间的元素使用默认的<进行排序
#include <algorithm> // 包含头文件
//自定义排序函数
bool cmp(const int a,const int b)
{ return a > b;}
int main()
{
int a[5]={3,4,1,2,5};
sort(a,a+5,cmp); //cmp,降序排序 5,4,3,2,1
sort(a,a+5); //默认升序排序,1,2,3,4,5
return 0;
}
//sort,自定义数据结构
#include <iostream>
#include <algorithm>
using namespace std;
struct Node
{
int x;
int y;
bool operator < (const struct Node& a) //重载<号
{
return x < a.x; //按x升序
//return x >a.x; //按x降序
}
}node[10];
int main()
{
node[0].x = 5;
node[1].x = 3;
node[2].x = 4;
node[3].x = 1;
node[4].x = 2;
sort(node,node+5);
for(int i = 0; i < 5; i++)
{
cout<<node[i].x<<endl; // 1 2 3 4 5
}
return 0;
}
相关文章推荐
- hdu 杭电 1728 逃离迷宫
- 并查集模板
- 《C++ Primer》第四版 第12章 类
- hdu 杭电 1253 胜利大逃亡
- hdu 杭电 2473 Junk-Mail Filter
- linked list cycle
- 杭电acm 题目分类
- 对于java的堆、栈的自己理解
- 用VLC读取摄像头产生RTSP流,DSS主动取流转发(一)
- - Pairs Forming LCM(算数基本定理)
- 《超实用的Node.js代码段》连载二:正确拼接Buffer
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
- 【错误积累】更新失败,数据包解压时出现错误!
- 思维-视野-沟通-动力-经验-表达
- 显著性水平 置信度 置信区间 实例讲解
- tomcat session实现原理
- 构建高性能web站点
- 20.5 语音合成(百度2016年2月29日发布的tts引擎)
- 用ImageLoader加载圆角图片(不是圆形)
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree