1、map的其中一个构造函数有第三个参数,可以直接定义map的key值得排序规则,
默认为std::less,即按“
map mapWord = { { "father", 1 },{ "mother", 4 },{ "daughter", 5 } };
等价于:
map> mapWord2 = { { "father", 1 },{ "mother", 4 },{ "daughter", 5 } };
2、如果想把key值按从大到小的顺序排序,改为:
map> mapWord2 ;
3、使用比较函数也可以
bool compFunc(const string& a, const string& b)
{
return a.compare(b) > 0;
}
map mapWord3; //注意*号的存在。比较操作类型必须为函数指针类型
4、使用lambda表达式
auto fc = [](const string& str1, const string& str2) {return str1.compare(str2) > 0; };
map mapWord4; //同样要使用使用函数指针
5、如果key的类型是自定义的类或结构体,可以重写“
classA{
...bool operator
{return count >d.count;
}intcount;
}
map mapA; //关键字将会从大向小排列