c++用vector对结构体排序

C++中,当 vector 中的数据类型为基本类型时,我们调用std::sort函数很容易实现 对vector中数据成员的升序或降序排序,然而当vector中的数据类型为自定义结构体类型或者类时,我们可以通过以下两种方法(仅以结构体为例):
  • 重载要排序的结构体或类的<号或者>号,演示代码如下:

#include<vector>
#include<algorIThm>
using namespace std;
typedef struct ValuePerWeight //定义结构体
{
    double vw;
    int i;
    bool operator <(const ValuePerWeight &other)const   //升序排序
    {
        return vw>other.vw;
    }
    //或者
    bool operator >(const ValuePerWeight &r)const   //降序排序
    {
        return vw>other.vw;
    }
}ValuePerWeight;
int main(){
    ValuePerWeight a1..an; 
    vector<ValuePerWeight> vpw ;
    for(int i=1;i<=n;i++){
        ....
        vpw.push_back(ai);//对a1到an处理后加到vpw
    }
    sort(vpw.begin(), vpw.end(),greater<ValuePerWeight>());//降序
    
    sort(vpw.begin(), vpw.end(),less<ValuePerWeight>());//升序
    //默认情况为升序 sort(vpw.begin(), vpw.end())
    ....
    return 0;
}

不修改结构体或类的定义部分,用函数对象来实现,演示代码如下:

#include<vector>
#include<algorIThm>
#include <functional> 

using namespace std;

typedef struct ValuePerWeight//自定义结构体
{
    double vw;
    int i;
    
}ValuePerWeight;
//全局函数
bool a_less_b(const ValuePerWeight& r,const ValuePerWeight& s)
{
    return r.vw < s.vw;
}
bool a_greater_b(const ValuePerWeight& r,const ValuePerWeight& s)
{
    return r.vw > s.vw;
}
int main(){
    ValuePerWeight a1..an; 
    vector<ValuePerWeight> vpw ;
    for(int i=1;i<=n;i++){
        ....
        vpw.push_back(ai);//对a1到an处理后加到vpw
    }
    sort(vpw.begin(), vpw.end(),a_less_b);   //升序排序
    
    sort(vpw.begin(), vpw.end(),a_greater_b); //降序排序

    ....
    return 0;
}



下面就是一个完整的,vector按多字段值进行排序的示例代码:下面就是一个完整的,vector按多字段值进行排序的示例代码:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;  

class Student
{
private:
	int id;			// 学号
	string name;		// 姓名
	float eyesight;		// 视力
	float height;		// 身高
	float chinese;		// 语文成绩
	float english;		// 英文成绩
	float math;		// 数学成绩
public:
	Student(int id, string name, float eyesight, float height, float chinese, float english, float math)
	{
		this->id = id;
		this->name = name;
		this->eyesight = eyesight;
		this->height = height;
		this->chinese = chinese;
		this->english = english;
		this->math = math;
	}

	int get_id()
	{
		return id;
	}

	string get_name()
	{
		return name;
	}

	float get_eyesight()
	{
		return eyesight;
	}

	float get_height()
	{
		return height;
	}

	float get_chinese()
	{
		return chinese;
	}

	float get_english()
	{
		return english;
	}

	float get_math()
	{
		return math;
	}
};

// 比较大小的函数(谓词)
bool comparer(Student& stu_a, Student& stu_b)
{
	// 按eyesight升序 + height升序排列
	if(stu_a.get_eyesight() != stu_b.get_eyesight())	
		return (stu_a.get_eyesight() < stu_b.get_eyesight());
	else
		return (stu_a.get_height() < stu_b.get_height());
	
	// 按eyesight降序° + height降序排列
	//if(stu_a.get_eyesight() != stu_b.get_eyesight())	
	//	return (stu_a.get_eyesight() > stu_b.get_eyesight());
	//else
	//	return (stu_a.get_height() > stu_b.get_height());

	// 按eyesight升序 + height降序排列
	//if(stu_a.get_eyesight() != stu_b.get_eyesight())	
	//	return (stu_a.get_eyesight() < stu_b.get_eyesight());
	//else
	//	return (stu_a.get_height() > stu_b.get_height());

	// 按eyesight降序 + height升序排列
	//if(stu_a.get_eyesight() != stu_b.get_eyesight())	
	//	return (stu_a.get_eyesight() > stu_b.get_eyesight());
	//else
	//	return (stu_a.get_height() < stu_b.get_height());
}

int main(int argc, char** argv)
{
	vector<Student> vec;
	vec.push_back(Student(4, "Dudley", 1.1f, 170.2f, 90.5f, 89.5f, 93.0));
	vec.push_back(Student(3, "Chris", 1.1f, 163.4f, 93.5f, 90.0f, 83.5f));
	vec.push_back(Student(2, "Bob", 1.5f, 166.6f, 86.0f, 98.5f, 85.0f));
	vec.push_back(Student(1, "Andrew", 1.5f, 173.2f, 98.5f, 100.0f, 100.f));

	// 调用STL中的sort函数,其中的第三个参数就是我们前面定义的,比较两个Student对象大小的函数
	sort(vec.begin(), vec.end(), comparer);

	vector<Student>::iterator iter;
	for(iter = vec.begin(); iter != vec.end(); ++iter)
	{
		cout << (*iter).get_eyesight() << "\t" << (*iter).get_height() << endl;
	}

	return 0;
}

实例三:
 
#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。











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值