zoukankan      html  css  js  c++  java
  • 数组结构体中排序 果枫

    //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;

    }

  • 相关阅读:
    一些至理名言
    移除快捷方式上面那个丑陋的小箭头
    一些浏览器插件
    yahoo给出的关于网站优化的建议
    javascript 事件流
    关于mongodb的一些笔记
    WebStorm
    给go添加各种package
    工具类 util.Date 日期类
    几种简单排序算法
  • 原文地址:https://www.cnblogs.com/zgfailmr/p/2665882.html
Copyright © 2011-2022 走看看