C++ vector 排序

C++中当 vector 中的数据类型为基本类型时我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现升序与降序排列呢?有两种方法,下面的例子能很好的说明:  方法1:
  我们直接来看代码吧,比较简单,容易理解:
   #include "stdafx.h"
   #include   <vector>   
   #include   <algorithm> 
   #include <functional> 
    
  using   namespace   std;   
  struct AssessTypeInfo

    unsigned int m_uiType;   //类型ID
    char   m_szName[64];  //类型名称
    unsigned int m_uiTotal;   //总分数

bool   operator <  (const   AssessTypeInfo&   rhs   )  const   //升序排序时必须写的函数
  {   
     return   m_uiType   <   rhs.m_uiType; 
   }
    bool   operator >  (const   AssessTypeInfo&   rhs   )  const   //降序排序时必须写的函数
   {   
       return   m_uiType   >   rhs.m_uiType; 
    }
}
int   main()   
  {   
   vector<AssessTypeInfo >   ctn   ;   
   
   AssessTypeInfo a1;
   a1.m_uiType=1;
   AssessTypeInfo  a2;
   a2.m_uiType=2;

AssessTypeInfo  a3;
   a3.m_uiType=3;

ctn.push_back(a1);
   ctn.push_back(a2);
   ctn.push_back(a3);
   //升序排序
   sort(ctn.begin(), ctn.end(),less<AssessTypeInfo>())   ;   //或者sort(ctn.begin(), ctn.end())  默认情况为升序
   
   for   ( int  i=0;   i<3;   i++   )   
    printf("%d\n",ctn[i].m_uiType);

//降序排序
  sort(ctn.begin(), ctn.end(),greater<AssessTypeInfo>())   ;

for   ( int  i=0;   i<3;   i++   )   
    printf("%d\n",ctn[i].m_uiType);   
   
   
   return   0  ;   
  }
以上方法就可以实现升序排序,输出结果为 1  2   3  
降序排序结果3  2  1。
方法2 :  不修改结构体或类的定义部分,我们用函数对象来实现:
  #include "stdafx.h"
  #include   <vector>   
  #include   <algorithm> 
  #include <functional> 
    
  using   namespace   std;   
struct AssessTypeInfo
{
unsigned int m_uiType;   //类型ID
  char   m_szName[64];  //类型名称
unsigned int m_uiTotal;   //总分数
};

bool   lessmark(const   AssessTypeInfo&   s1,const   AssessTypeInfo&   s2)   
  {   
      return   s1.m_uiType   <   s2.m_uiType;   
  } 
  bool   greatermark(const   AssessTypeInfo&   s1,const   AssessTypeInfo&   s2)   
  {   
      return   s1.m_uiType   >   s2.m_uiType;   
  } 
int   main()   
  {   
   vector<AssessTypeInfo >   ctn   ;   
   
   AssessTypeInfo a1;
   a1.m_uiType=1;
   AssessTypeInfo  a2;
   a2.m_uiType=2;

AssessTypeInfo  a3;
   a3.m_uiType=3;

ctn.push_back(a1);
   ctn.push_back(a2);
   ctn.push_back(a3);

sort(ctn.begin(), ctn.end(),lessmark)   ;   //升序排序
   
   
   for   ( int  i=0;   i<3;   i++   )   
    printf("%d\n",ctn[i].m_uiType);   
   
   sort(ctn.begin(), ctn.end(),greatermark)   ;   //降序排序

return   0  ;   
  }

C++ vector 排序的更多相关文章

  1. 1016. Phone Bills (25) -vector排序(sort函数)

    题目如下: A long-distance telephone company charges its customers by the following rules: Making a long- ...

  2. 2.2 C语言_实现数据容器vector(排序功能)

    上一节我们说到我们己经实现了一般Vector可以做到的自动扩充,告诉随机存取,那么现在我们需要完成vector的一个排序的功能. 排序算法我们网上一百度哇~~!很常见的就有8大排序算法: 1.选择排序 ...

  3. NX二次开发-C++的vector排序去重用法

    #include <algorithm> //vector排序去重 sort( BoxNum.begin(), BoxNum.end()); BoxNum.erase(unique(Box ...

  4. STL之使用vector排序

    应用场景: 在内存中维持一个有序的vector: // VectorSort.cpp : Defines the entry point for the console application. #i ...

  5. C++中的结构体vector排序

    在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: // sort algorithm example #include ...

  6. vector排序

    // VectorSort.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...

  7. java.util.Vector排序

    Vector的排序: import java.util.*; class MyCompare implements Comparator //实现Comparator,定义自己的比较方法{public ...

  8. vector 排序

    #include <vector> #include <algorithm> 一.vector保存的是基础数据类型(int.char.float等) vector<int ...

  9. C++标准库 vector排序

    前天要做一个对C++ STL的vector容器做一个排序操作,之前一直把vector当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...

随机推荐

  1. 201621123018《java程序设计》第13周作业总结

    1. 本周学习总结 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网络同时使用,需要为你的系统增加网络功能. 2.1 简述你想为你的系统增加什 ...

  2. 防止sql注入的小函数 以及一些小验证

    function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialch ...

  3. Spring boot mybatis : Error creating bean with name 'com.github.pagehelper.autoconfigure.MapperAutoConfiguration': Invocation of init method failed;

    报错截图: 解决方法: 只能扫描到自定义的mapper,不能扫描到其他文件. @MapperScan("com.streamax.s17.tms.dao.pper.repository&qu ...

  4. 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件时,常常提示“有未能满足的依赖关系“,解决方法

    很早之前在ubuntu安装软件时遇到的问题,今天打开ubuntu看到了,总结如下: 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件常常提示“有 ...

  5. python互斥锁

    互斥锁 进程之间数据隔离, 但是多个进程可以共享同一块数据,比如共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,而共享带来的是竞争,竞争带来的结果就是错乱,如下 from mu ...

  6. [LeetCode] Z字型变换

    题目内容: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:" ...

  7. K-means算法的原理、优缺点及改进(转)

    文章内容转载自:http://blog.csdn.net/sinat_35512245/article/details/55051306                                ...

  8. JDK8 - Function介绍

    注:写这个文档只是为了方便加深记忆,加强理解,重点关注两个default方法中泛型[V]. JDK8作为一个还在维护阶段的长期版本,势必会在企业应用中占据相当大的市场份额,所以还是以JDK8作为例子的 ...

  9. Android的几种弹出框

    项目效果图: 新建一个项目,结构图如下所示: activity_main.xml: <?xml version="1.0" encoding="utf-8" ...

  10. PLSQL Developer是什么?

    不多说,直接上干货! PLSQL Developer是一款可以帮助用户管理Oracle数据库开发存储程序单元的集成开发环境IDE,通过该软件,用户可以编辑.编译.纠正.测试.调试.优化.查询您的数据信 ...