typedef map<string, string> myMap;
Run Code Online (Sandbox Code Playgroud)
插入新对时myMap,它将使用键string通过自己的字符串比较器进行比较.是否可以覆盖该比较器?例如,我想比较密钥string的长度,而不是字母表.或者还有其他方法可以对地图进行排序吗?
Geo*_*che 123
std::map最多需要四个模板类型参数,第三个是比较器.例如:
struct cmpByStringLength {
bool operator()(const std::string& a, const std::string& b) const {
return a.length() < b.length();
}
};
// ...
std::map<std::string, std::string, cmpByStringLength> myMap;
Run Code Online (Sandbox Code Playgroud)
或者,您也可以将比较器传递给maps构造函数.
但请注意,按长度比较时,地图中每个长度只能有一个字符串作为键.
Joh*_*ing 12
是的,第3个模板参数on map指定比较器,它是二进制谓词.例:
struct ByLength : public std::binary_function<string, string, bool>
{
bool operator()(const string& lhs, const string& rhs) const
{
return lhs.length() < rhs.length();
}
};
int main()
{
typedef map<string, string, ByLength> lenmap;
lenmap mymap;
mymap["one"] = "one";
mymap["a"] = "a";
mymap["fewbahr"] = "foobar";
for( lenmap::const_iterator it = mymap.begin(), end = mymap.end(); it != end; ++it )
cout << it->first << "\n";
}
Run Code Online (Sandbox Code Playgroud)
hon*_*onk 11
从C++ 11开始,您还可以使用lambda表达式而不是定义比较器结构:
auto comp = [](const string& a, const string& b) { return a.length() < b.length(); };
map<string, string, decltype(comp)> my_map(comp);
my_map["1"] = "a";
my_map["three"] = "b";
my_map["two"] = "c";
my_map["fouuur"] = "d";
for(auto const &kv : my_map)
cout << kv.first << endl;
Run Code Online (Sandbox Code Playgroud)
输出:
1
两
三个
fouuur
我想重复Georg的答案的最后一点:当按长度比较时,你只能在地图中将每个长度的一个字符串作为键.