C++ map 自定义排序规则

map的模板定义第三个参数,即为我们的排序规则。

默认是试用std::less<>排序的,对key排序,而不是value。

如果我们想对key (string)不区分大小写进行排序,可以这样写。


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


// 提供自定义的排序
struct CCaseInsensitive{
	bool operator() (const string& str1, const string& str2) const{
		string str1NoCase(str1);
		string str2NoCase(str2);
		transform(str1.begin(), str1.end(), str1NoCase.begin(), tolower);
		transform(str2.begin(), str2.end(), str2NoCase.begin(), tolower);
		return str1NoCase < str2NoCase;
	}
};

//定义StrMapNoCase
typedef map<string, string, CCaseInsensitive> StrMapNoCase;
typedef StrMapNoCase::value_type MapValue;
typedef map<string, string> StrMap;



int main(){
	StrMapNoCase map1;
	map1.insert(MapValue("John", "5214563"));
	map1.insert(MapValue("tom", "5214563"));
	map1.insert(MapValue("LiYang", "98874745"));
	map1.insert(MapValue("ABC", "98874745"));
	
	StrMap map2;
	map2.insert(MapValue("John", "5214563"));
	map2.insert(MapValue("tom", "5214563"));
	map2.insert(MapValue("LiYang", "98874745"));
	map2.insert(MapValue("ABC", "98874745"));
	
	StrMapNoCase::iterator iter1 = map1.find("abc");
	if( iter1 != map1.end())
		cout << (*iter1).first <<endl;


	StrMap::iterator iter2 = map2.find("abc");
	if(iter2 != map2.end())
		cout << (*iter2).first <<endl;
	else
		cout << "map2中没有找到abc" << endl;

	system("pause");
	return 0;
}

运行结果:

ABC

map2中没有找到abc


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++,std::map是一个有序的关联容器,它的元素按照键的大小进行排序。如果想要自定义排序规则,可以使用自定义比较函数或者lambda表达式来实现。 引用的代码示例展示了如何利用自定义比较函数来对std::map进行排序。首先,将std::map的键值对拷贝到一个std::vector,然后使用std::sort函数对std::vector进行排序。在自定义比较函数cmp_val,我们定义了按照键对应的值进行排序规则。最后,通过遍历排序后的std::vector来输出排序后的结果。 引用的代码示例展示了使用lambda表达式来实现相同的功能。在std::sort函数的第三个参数,我们使用了一个lambda表达式来定义排序规则,即按照键对应的值进行排序。 以下是一个类似的示例代码,用于自定义排序std::map: ```cpp #include <iostream> #include <string> #include <map> #include <algorithm> #include <vector> using namespace std; typedef pair<string, int> PAIR; struct cmp { bool operator()(const PAIR& left, const PAIR& right) const { return left.second < right.second; } }; int main() { map<string, int> ma; ma["Alice"] = 86; ma["Bob"] = 78; ma["Zip"] = 92; ma["Stdevn"] = 88; vector<PAIR> vec(ma.begin(), ma.end()); sort(vec.begin(), vec.end(), cmp()); for (vector<PAIR>::iterator ite = vec.begin(); ite != vec.end(); ++ite) { cout << ite->first << " " << ite->second << endl; } return 0; } ``` 在这个示例,我们定义了一个结构体cmp作为自定义比较函数,并在main函数创建一个map对象ma,然后将键值对拷贝到一个vector,并使用自定义比较函数cmp对vector进行排序。最后,通过遍历排序后的vector来输出结果。 请注意,这只是一个示例代码,你可以根据实际需求进行修改和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++map自定义排序](https://blog.csdn.net/sinat_31608641/article/details/128122868)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ map自定义排序](https://blog.csdn.net/frighting_ing/article/details/122027936)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值