【发布时间】:2013-06-26 18:47:16
【问题描述】:
我在 std::map 中自定义比较如下。
class my_cmp {
public:
bool operator()(const string &a, const string &b) {
return (a.length() >= b.length());
}
};
map<string, int, tmp_cmp> tmp; 适用于按字符串长度排序的所有键。但是tmp.erase("a string"); 不再有效。是否有解决方案使 std::map::erase(key_type) 在自定义比较后仍然有效?
【问题讨论】:
-
我的地图键可以有相同的长度。这就是我所期望的。但即使我改成
class my_cmp { public: bool operator()(const string &a, const string &b) { if (a.length() == b.length()) return (a < b); else return (a.length() > b.length()); } };
标签: c++