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. 【BZOJ1053】 反素数ant

    BZOJ1053 反素数ant 我们先考虑唯一分解定理求出约数个数: \(x=a_1^{p_1}a_2^{p_2}a_3^{p_3}...a_k^{p_k}\) 然后\(num=\Pi_{i=1}^k ...

  2. 【062新题】OCP 12c 062出现大量新题-15

    choose one In your Oracle 12c database, you plan to execute the command: SQL> CREATE TABLESPACE t ...

  3. Linux例行工作与系统管理(13)

    Linux 系统的任务是由cron(crond)这个系统服务来控制的,Linux系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外,由于使用者自己也可以设置计划任务,所以Linux ...

  4. git log 高级用法

    转自:https://github.com/geeeeeeeeek/git-recipes/wiki/5.3-Git-log%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95 内 ...

  5. Linux链接脚本学习--lds(转)

    Linux链接脚本学习--lds 一.概论 ld: GNU的链接器. 用来把一定量的目标文件跟档案文件链接在一起,并重新定位它们的数据,链接符号引用. 一般编译一个程序时,最后一步就是运行ld进行链接 ...

  6. Info - Get technical information from the Internet

    Official Sites Overview / QuickStart Guide / Docs / E-books Community / Fourm / Blog Demo / Download ...

  7. JavaScript(JS)简介

    历史背景介绍 (Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript ...

  8. python传输文件

    传输文件简单版 server端: import socket import struct import json import os share_dir = r'C:\py3Project\路飞\第三 ...

  9. Nginx配置SSL自签名证书

    生成自签名SSL证书 生成RSA密钥(过程需要设置一个密码,记住这个密码) $ openssl genrsa -des3 -out domain.key 1024 拷贝一个不需要输入密码的密钥文件 $ ...

  10. 一、Linq简介

    语言集成查询Language Integrated Query(LINQ)是一系列将查询功能集成到C#语言的技术统称. 传统数据查询的缺点: 简单的字符串查询,没有编译时类型检查或Intellisen ...