C++ vector 排序
2013-02-22 12:28
260 查看
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 ;
}
以上方法就可以实现升序排序,输出结果为 1 2 3
降序排序结果3 2 1。
方法2是一种比较简单的方法。
以上两种方法您可根据您自己的需求选择,并且以上两种方法在VC++6.0环境下编译通过,也是自己在实践过程中的总结,如有不妥的地方,欢迎您指出,至于为什么这样使用,请参考 stl算法中sort 部分。
方法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 ;
}
以上方法就可以实现升序排序,输出结果为 1 2 3
降序排序结果3 2 1。
方法2是一种比较简单的方法。
以上两种方法您可根据您自己的需求选择,并且以上两种方法在VC++6.0环境下编译通过,也是自己在实践过程中的总结,如有不妥的地方,欢迎您指出,至于为什么这样使用,请参考 stl算法中sort 部分。
相关文章推荐
- C++ vector 排序
- C++ vector 排序
- C++STL中,map/multimap,set/multiset 和vector的排序
- 【转】c++中Vector等STL容器的自定义排序
- C++ vector 排序(转载)
- C++中的结构体vector排序
- C++中vector的排序问题
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- c++中vector排序的问题二(转载)
- c++用vector对结构体排序
- c++map按value排序--将map的pair对保存到vector中,然后写比较仿函数+sort完成排序过程。
- C++ 中对vector<T*> 数组的查找和排序
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- C++ vector容器使用 排序
- C++排序之数组,Vector,priority_queue
- C++ 对vector进行排序
- C++ vector容器使用 排序
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- 实战c++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数出错“应输入 2 个参数,却提供了 3 个)
- C++中,自定义结构体vector的排序