C++ vector 排序

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

时间: 2024-12-13 03:47:45

C++ vector 排序的相关文章

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

题目如下: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance

vector排序与查找

本示例使用比较函数,函数对象进行vector的排序与查找操作. #include <algorithm> #include <vector> using namespace std; #define MAX_NAME_LEN 32 struct Student { int no; char name[MAX_NAME_LEN]; int age; bool operator==(const Student& other) const { return no == other

C++中的结构体vector排序

在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: 1 // sort algorithm example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::sort 4 #include <vector> // std::vector 5 6 bool myfunction (int i,int j) { re

STL之使用vector排序

应用场景: 在内存中维持一个有序的vector: 1 // VectorSort.cpp : Defines the entry point for the console application. 2 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 7 //先自定义一个结构体 8 struct Test { 9 float member1; 10 std::string member

vector 排序

http://classfoo.com/ccby/article/cIBahI 排序 sort 同 stable_sort #include <vector> #include <algorithm> #include <functional> // For greater<int>() #include <iostream> int main() { vector <int> foo; vector <int>::ite

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

上一节我们说到我们己经实现了一般Vector可以做到的自动扩充,告诉随机存取,那么现在我们需要完成vector的一个排序的功能. 排序算法我们网上一百度哇~~!很常见的就有8大排序算法: 1.选择排序 2.冒泡排序 3.插入排序 4.快速排序 5.归并排序 6.桶排序 7.堆排序 8.希尔排序 具体的思想本猿就不展开讲啦,现在C语言应用的场景大多数在服务器和嵌入式设备,服务器数据量大,嵌入式设备资源有限 两者是对时间复杂度和空间负责度的两个极端. 一开始我想要优化堆排序,使得堆排序的空间复杂度减

CF 496D(Tennis Game-O(t*(n/t)复杂度+vector排序)

D. Tennis Game time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of mult

NX二次开发-算法篇-vector函数排序(例子:遍历所有点并排序)

1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_ui.h> 5 #include <uf_curve.h> 6 #include <uf_obj.h> 7 #include <uf_part.h> 8 #include <vector> 9 #include <algorithm> 10 11 using namespace std; 12 13 14 UF_initiali

C++ STL学习——vector

学过C++的人肯定会很熟悉STL标准模板库,STL其实就是封装了一系列的接口,供我们调用.很多函数或者算法的实现不需要我们从头开始写,大大提高我们的编程效率.这篇博客在简单介绍STL的情况下,会详细的来介绍vector的使用. STL共有六大组件: 一.容器(Container):是一种数据结构,如list,vector,deque,queue等,以模板类的方法提供,为了访问容器中的数据,可以使用由容器类提供的迭代器. 二.迭代器(Iterator):提供了访问容器中对象的方法. 三.算法(Al