don*_*ius 4 c++ stl operator-overloading c++11 reference-wrapper
我无法弄清楚如何使用a 获得std::string引用.根据以下链接,我知道我需要超载.std::unordered_mapstd::reference_wrapperoperator==
为什么不能在`std :: reference_wrapper`s中推导出模板实例?
但是,我无法弄清楚如何编写operator==它将需要一个const std::reference_wrapper.如果包装器不是const,那就没问题了.
使用char代替std::string工作正常(不需要重载operator==).
码:
#include <iostream>
#include <unordered_map>
#include <functional>
bool operator==(const std::reference_wrapper<std::string> lhs,
const std::reference_wrapper<std::string> rhs)
{
return std::equal_to<std::string>()(lhs.get(), rhs.get());
}
int main(){
char chr('a');
std::string str("b");
int num(1);
// this works (char)
std::unordered_map<std::reference_wrapper<char>, int, std::hash<char>> charMap;
std::pair<std::reference_wrapper<char>, int> charPair(chr , num);
charMap.insert(charPair);
std::cout << "charMap works. Output: " << charMap[chr] << std::endl;
// does not work (std::string)
std::unordered_map<std::reference_wrapper<std::string>, int, std::hash<std::string>> stringMap;
std::pair<std::reference_wrapper<std::string>, int> stringPair(str , num);
stringMap.insert(stringPair); // compile error
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
error: no match for ‘operator==’ (operand types are ‘const std::reference_wrapper<std::__cxx11::basic_string<char> >’ and ‘const std::reference_wrapper<std::__cxx11::basic_string<char> >’)
{ return __x == __y; }
Run Code Online (Sandbox Code Playgroud)
operator==对于非用户定义的类型,您不能提供自己的重载.也就是说,充其量是未定义的行为.但是,您不需要在此处执行此操作.std::unordered_map有五个模板参数:
template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;
Run Code Online (Sandbox Code Playgroud)
看到第四个?那正是你想要的.您需要提供一个比较功能.幸运的是,您可以像这样使用std :: hash和std :: equal_to:
std::unordered_map<
std::reference_wrapper<std::string>,
int,
std::hash<std::string>,
std::equal_to<std::string>
> stringMap;
Run Code Online (Sandbox Code Playgroud)