vector中sort排序(解决char类型数据无法排序的问题)

来源:互联网 发布:怎么在淘宝上面买假证 编辑:程序博客网 时间:2024/05/16 17:38

C++中当 vector 中的数据类型为基本类型时我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现升序与降序排列呢?有两种方法,下面的例子能很好的说明:

 

方法一:在自定义结构体中重载<、>

 

#include<vector>   
#include<algorithm> 
#include<iostream>
    
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( int argc, char* argv[] ) 
  
   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++ )

       cout<<ctn[i].m_uiType<<endl;


  //降序排序

  sort(ctn.begin(), ctn.end(),greater<AssessTypeInfo>())   ;  


   for( int  i = 0;   i < 3;   i++   )   
      cout<<ctn[i].m_uiType<<endl;   


   return   0;   
  }

 

方法2 :  不修改结构体或类的定义部分,我们用函数对象来实现:

 

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

typedef struct tStudent
{
        char name[ 10 + 1 ];
        string region;
        long id;
}Student;

bool Comp( const Student &a, const Student &b )
{
     if( string(a.name) != string(b.name) )
           return string(a.name) < string(b.name);
     if( a.region != b.region )
         return a.region < b.region;
     
     if( a.id != b.id )
         return a.id < b.id;
}

int main( int argc, char* argv[] )
{
    vector<Student> vA;
    vector<Student> vB;
    
    Student a = { "aaaa", "beijing", 1 };
    Student b = { "aaaa", "nanjing", 10 };
    Student c = { "bbbb", "shanghai", 1 };
    Student d = { "cccc", "xian", 1 };
    Student e = { "cccc", "taiyuan", 100 };
    
    vA.push_back( b );
    vA.push_back( e );
    vA.push_back( a );
    vA.push_back( c );
    vA.push_back( d );
    
    
    cout<<"sort begin:"<<endl;
    for( int i = 0; i < vA.size(); i++ )
    {
         cout<<vA[i].name<<" | "<<vA[i].region<<" | "<<vA[i].id<<endl;
    }
    
    sort( vA.begin(), vA.end(), Comp );
    
    cout<<endl<<"sort back:"<<endl;
    for( int i = 0; i < vA.size(); i++ )
    {
         cout<<vA[i].name<<" | "<<vA[i].region<<" | "<<vA[i].id<<endl;
    }
    
    system( "pause" );
    return 0;
}

方法2是一种比较简单的排序方法。

 

sort采用的是成熟的"快速排序算法"(目前大部分STL版本已经不是采用简单的快速排序,而是结合内插排序算法)。可以保证很好的平均性能、复杂度为n*log(n)。

 

qsort()的速度远远慢于<algorithm>中的sort()。主要原因就是那第4个参数,因为编译器不可能内联通过函数指针传递的函数,所以在qsort()中每次判断都会真正地调用一次比较函数。而在sort()中传递的是“仿函数”,这是一个行为类似函数的对象(通过重载operator()实现),而当“调用”它时,编译器可以轻易内联其代码,于是可以省略大量的函数调用,节约时间。

原创粉丝点击