首页 文章

在std :: unordered_map中重载operator == for const std :: reference_wrapper

提问于
浏览
4

我无法弄清楚如何使用 std::reference_wrapperstd::string 引用转换为 std::unordered_map . 根据以下链接,我了解到我需要重载 operator== .

Why can template instances not be deduced in std::reference_wrappers?

但是,我无法弄清楚如何写 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
}

编译错误:

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; }

1 回答

  • 8

    对于非用户定义类型,您无法为 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;
    

    看到第四个?那正是你想要的 . 您需要提供一个比较功能 . 幸运的是,您可以像这样使用std :: hash和std :: equal_to:

    std::unordered_map<
        std::reference_wrapper<std::string>,
        int,
        std::hash<std::string>,
        std::equal_to<std::string>
    > stringMap;
    

相关问题