用sort 排序 struct

+++

//method 1
struct node{
int k,s;
}p[5005]; bool cmp1(node x,node y){
return x.s>y.s; //定义降序排序(从大到小)
}
bool cmp2(node x,node y){
return x.k<y.k; //定义升序排序(从小到大)
}sort(p+1,p+n+1,cmp2); //排序
eg:
#include<iostream>
#include<algorithm> using namespace std; struct b {
int k, s;
}; bool compare(b x, b y)
{
if (x.k != y.k) return x.k > y.k;
return x.s > y.s;//降序排序
} int main()
{
b p[5] = { {2, 4}, {2, 8}, {5, 9}, {1, 4}, {2, 5} };
sort(p, p + 5, compare);//point
for (auto x : p)
cout << x.k << " " << x.s << endl;
system("pause");
return 0;
}
//method 2
struct b {
int k, s;
bool operator< (const b & t)const//实现降序排序
{
if (k != t.k) return k < t.k;
return s < t.s;
}
};
eg:
#include<iostream>
#include<algorithm> using namespace std; struct b {
int k, s;
bool operator< (const b & t)const
{
if (k != t.k) return k < t.k;
return s < t.s;
}
}; int main()
{
b p[5] = { {2, 4}, {2, 8}, {5, 9}, {1, 4}, {2, 5} };
sort(p, p + 5);//参数只需要两个迭代器,不需要第三个参数
for (auto x : p)
cout << x.k << " " << x.s << endl;
system("pause");
return 0;
}

用sort实现对struct的排序的更多相关文章

  1. Hadoop 实现对Value倒序排序

    数据源 A B C D Z 要实现的输出 Z D B C A 看字符顺序,其实什么也没有,只是按照后面的数字进行一次倒序排序,实现思路,1利用hadoop自带的排序功能,2.KV互换 实现代码 pub ...

  2. C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客

    C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客 C++中实现对map按照value值进行排序 2012-03-15 15:32:36 标签:map 职场 休闲 排 ...

  3. 使用泛型实现对int数组或者String数组进行排序

    因为是使用的泛型,我们并不确定数据类型, 对于数据的比较就不能用平时的大于或者小于. 我们需要比较对象实现Comparable接口,该接口下的compareTo()方法可以用来比大小 定义Sort类: ...

  4. C#代码实现对HTTP POST参数进行排序

    private static string GetSortedParas(Dictionary<string, string> dic) { dic = dic.OrderBy(key = ...

  5. 使用代理实现对C# list distinct操作

    范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复.而一般的如果要对list去重复如果使用linq dist ...

  6. 在应用程序中实现对NandFlash的操作

    以TC58NVG2S3ETA00 为例: 下面是它的一些物理参数: 图一 图二 图三 图四 图五 图6-0 图6-1 说明一下,在图6-1中中间的那个布局表可以看做是实际的NandFlash一页数据的 ...

  7. sort+结构体实现二级排序

    之前介绍的sort函数由于其效率较高,使用较为简单让我用起来那叫一个爽,今天再写一篇使用sort+结构体实现二级排序的方法. 还是先想个问题吧,比如我想输入5个同学的名字和身高,然后得到他们身高的降序 ...

  8. <algorithm>里的sort函数对结构体排序

    题目描述 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请根据记录找出当天开门和关门的人. 输入描述: 每天的记录在第一行给出记录的条目数M (M &g ...

  9. 实现对DataGird控件的绑定操作

    //实现对DataGird控件的绑定操作 function InitGrid(queryData) { $('#grid').datagrid({ //定位到Table标签,Table标签的ID是gr ...

随机推荐

  1. Django模型中的admin后台管理无法显示字段

    在执行django后台管理时,登陆到http://127.0.0.1:8000/admin/,进入页面后没有对应的字段显示.请解决? 代码: models.py from django.db impo ...

  2. Webpack 一,打包JS

    创建入口文件 app.js // es6 module 规范 import sum_d from './sum.js' import {sum_e} from './sum.js' // commco ...

  3. Codeforces_799

    A.求两个时间比较一下. #include<bits/stdc++.h> using namespace std; int n,t,k,d; int main() { ios::sync_ ...

  4. BZOJ2038 小Z的袜子(莫队之源)

    题意+思路: 给你m个区间询问,问每个区间内的$\displaystyle \frac{\sum x^2-(R-L+1)}{(R-L)(R-L+1)} $,其中x为每种数字的个数,用cnt存储: 所以 ...

  5. 《N诺机试指南》(五)进制转化

    进制转化类题目类型: 代码详解及注释解答:  //进制转化问题 #include <bits/stdc++.h> using namespace std; int main(){ // 1 ...

  6. 快速幂——while理解&&[P1965] 转圈游戏

    快速幂--while理解 \[a^k\] 把k转成2进制 \[k=2^n*p[n]+2^(n-1)*p[n-1]+...+2^1*p[1]+2^0*p[0]\] \[a^k=a^(2^n*p[n]+2 ...

  7. LeetCode 684. Redundant Connection 冗余连接(C++/Java)

    题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given in ...

  8. 杭电-------2052Picture(C语言)

    #include<stdio.h> int main() { int width, height; int i, j; while (~scanf("%d %d", & ...

  9. Apache 的多站点配置

    1.修改httpd.conf 文件 Apache的主配置文件路径: D:\phpTools\Apache24\conf 用编辑器打开 httpd.conf 文件,查找 #Include conf/ex ...

  10. vue垂死挣扎--遇到的问题

    1, 原生js监听浏览器后退及禁用返回 +. 涉及到的history的知识 2, watch监听路由变化