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. 【计算机网络】 网络体系结构分类: 客户机/服务器体系和P2P

    网络体系结构的分类 现代网络应用程序有两种主流的体系结构: 客户机/服务器体系结构和P2P体系结构(peer to peer “对等”)   一 . 客户机/服务器体系结构     客户机/服务器体系 ...

  2. [JavaScript] 跳出循环方法总结

    1.forEach() 方法对数组的每个元素执行一次提供的函数.但是没有办法中止或者跳出 forEach 循环,除了抛出一个异常,该方法没有返回值,return/return false/return ...

  3. Django中安装搜索引擎方法。

    全文检索 全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理. haystack:全文检索的框架,支持whoosh.solr.Xapian.Elasticsearc ...

  4. 12-02 java String类

    String构造方法 package cn.itcast_01; /* * 字符串:就是由多个字符组成的一串数据.也可以看成是一个字符数组. * 通过查看API,我们可以知道 * A:字符串字面值&q ...

  5. [Umbraco] Data Type的扩展编程

    继续从上面的Data Types的自定义控件说起.前面用到了自定义控件的数据绑定,虽然这使得我们可以调用外部数据了,但这似乎还比较死板,如果再调用其他数据,还得再创建一个控件,那样的话就会出现类似的功 ...

  6. MVC3学习:Sql Server2005中时间类型DateTime的显示

    在Sql Server2005中,如果将某字段定义成日期时间类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如 2013/8/6 13:37:33) 如果只想显示成年月日形式,不要时 ...

  7. 一口一口吃掉Hexo(五)

    如果你想得到更好的阅读效果,请访问我的个人网站 ,版权所有,未经许可不得转载! 通过前四节的内容,相信你已经能够在你的虚拟主机上成功部署网站,并且能够通过你自己的域名访问你的网站了,接下来要做的就是日 ...

  8. Android 开发工具类 35_PatchUtils

    增量更新工具类[https://github.com/cundong/SmartAppUpdates] import java.io.File; import android.app.Activity ...

  9. php -- 格式化字符串

    ----- 003-output.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  10. Vue笔记:使用 axios 发送请求

    在Vue1.0的时候有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource. 关于为什么放弃推荐? -> 尤 ...