std::sort() 如何自定义比较函数 进行排序 。

/*

std::sort() 如何自定义比较函数 进行排序 。

Parameters
first, last	-	the range of elements to sort
policy	-	the execution policy to use. See execution policy for details.
comp	-	comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ​true if the first argument is less than (i.e. is ordered before) the second. 
The signature of the comparison function should be equivalent to the following:

//比较函数定义规则如下
bool cmp(const Type1 &a, const Type2 &b);

The signature does not need to have const &, but the function object must not modify the objects passed to it.
The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. ​


*/

#include "stdafx.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace  std;

struct STest
{
	int m_age;
	std::string m_strName;
};

typedef std::vector<STest>CVector;

//升序
bool compareAsc(const STest &value1,const STest &value2)
{
	return value1.m_age > value2.m_age;
}

//降序
bool compareDes(const STest &value1,const STest &value2)
{
	return value1.m_age < value2.m_age;
}


int _tmain(int argc, _TCHAR* argv[])
{
	std::cout<<"原始数据\n";
	CVector vecTest;
	for (int i = 0; i < 10; i++)
	{
		STest tmp;
		tmp.m_age = i;
		char ch[260]={0};
		sprintf(ch,"%dhello",i);
		tmp.m_strName = std::string(ch);
		vecTest.push_back(tmp);
		std::cout<<"age: "<<tmp.m_age <<"\tname: "<<tmp.m_strName.c_str()<<endl;
	}

	std::cout<<"==============================\n";
	std::cout<<"升序序排序\n";
	std::sort(vecTest.begin(),vecTest.end(),compareAsc);
	CVector::iterator iter = vecTest.begin();
	for (iter; iter != vecTest.end(); iter++)
	{
		std::cout<<"age: "<<iter->m_age <<"\tname: "<<iter->m_strName.c_str()<<endl;
	}

	std::cout<<"==============================\n";
	std::cout<<"降序排序\n";
	std::sort(vecTest.begin(),vecTest.end(),compareDes);
	iter = vecTest.begin();
	for (iter; iter != vecTest.end(); iter++)
	{
		std::cout<<"age: "<<iter->m_age <<"\tname: "<<iter->m_strName.c_str()<<endl;
	}
	return 0;
}


  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
当中的数据类型为自定义结构体类型时,可以通过两种方法实现升序与降序排列。 方法一是通过定义一个比较函数对象,即重载小于运算符( operator< ),来实现自定义排序比较函数对象需要接受两个参数,分别是待比较的两个结构体对象。在比较函数中,根据自定义排序规则,比较结构体对象的某个成员变量,并返回比较结果。然后,将比较函数对象作为第三个参数传递给std::sort函数即可。这样,std::sort函数就会按照自定义排序规则对vector中的元素进行排序。 方法二是通过定义一个lambda表达式来实现自定义排序。lambda表达式是一种匿名函数,可以在代码中内联定义,非常方便。在lambda表达式中,可以通过捕获列表来访问外部变量,并定义排序规则。然后,将lambda表达式作为第三个参数传递给std::sort函数即可。这样,std::sort函数就会按照自定义排序规则对vector中的元素进行排序。 综上所述,通过定义一个比较函数对象或者lambda表达式,可以实现std::sort方法的自定义排序。<span class="em">1</span> #### 引用[.reference_title] - *1* [vector 排序](https://download.csdn.net/download/yanwuoxuao/3601529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值