Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载标准::字符串小于c+中的运算符+;_C++_String_Stl_Operator Overloading - Fatal编程技术网

C++ 重载标准::字符串小于c+中的运算符+;

C++ 重载标准::字符串小于c+中的运算符+;,c++,string,stl,operator-overloading,C++,String,Stl,Operator Overloading,我想使用map来存储字符串及其出现时间 由于要求,我必须重载“不,这不是正确的方法 您应该定义一个自定义函子,该函子可以进行所需的比较。请在创建映射时使用它 struct MyCustomFunctor { bool operator()(const string& a, const string& b) { /* Add the details */ } }; 然后,使用以下方法创建地图: map<string, int, MyCustomFunctor>

我想使用
map
来存储字符串及其出现时间


由于要求,我必须重载“不,这不是正确的方法

您应该定义一个自定义函子,该函子可以进行所需的比较。请在创建映射时使用它

struct MyCustomFunctor
{
    bool operator()(const string& a, const string& b) { /* Add the details */ }
};
然后,使用以下方法创建地图:

map<string, int, MyCustomFunctor> myMap;

这就是为什么订购了容器。您是如何创建地图的?您是否以某种方式告诉它应该使用您的自定义运算符?此外,您确定要在任何地方使用它(不知不觉地)比较字符串?是的,这就是我想做的。根据我的顺序来绘制地图,而不是你的操作符违反严格的总排序要求的词典。具体来说,你有两个
“a”<“bb”
“bb”<“a”
。嗨,molbdnilo,不,这是严格的排序,“a”<“bb”与ListIIORY顺序的区别是“B”和“BB”的情况,在现代C++中,“B”>“BB”,lambda函数可能更适合创建一个全新的类,这就是为什么在C++ 11中,使用模板对象的函数对象更容易。
map<string, int, MyCustomFunctor> myMap;
auto lambda = [](const string& a, const string& b) -> bool {return (a < b);};
map<string, int, decltype(lambda)> myMap(lambda);
auto it = myMap.begin();
auto it = myMap.find("some key");