【问题标题】:How to Use std::unordered_map with std::pair?如何将 std::unordered_map 与 std::pair 一起使用?
【发布时间】:2020-04-09 19:49:50
【问题描述】:

我有这个错误:

binary '==': 'std::pair' 未定义此运算符或转换为预定义运算符可接受的类型*

在这段代码中:

using EntityId = unsigned int;
using ComponentContainer = std::vector<C_Base*>;
using EntityData = std::pair<Bitmask,ComponentContainer>;

// ERROR below at line:
using EntityContainer = std::unordered_map<EntityId, EntityData>;

这是C_Base 类:

#include <iostream>
#include <sstream>
#include "ECS_Types.h"

class C_Base{
public:
    C_Base(const Component& l_type): m_type(l_type){}
    virtual ~C_Base(){}

    Component GetType(){ return m_type; }

    friend std::stringstream& operator >>(
        std::stringstream& l_stream, C_Base& b)
    {
        b.ReadIn(l_stream);
        return l_stream;
    }

    virtual void ReadIn(std::stringstream& l_stream) = 0;
protected:
    Component m_type;
};

还有Bitmask 类:

#include <stdint.h>

using Bitset = uint32_t;

class Bitmask{
public:
    Bitmask() : bits(0){}
    Bitmask(const Bitset& l_bits) : bits(l_bits){}

    Bitset GetMask() const{ return bits; }
    void SetMask(const Bitset& l_value){ bits = l_value; }

    bool Matches(const Bitmask& l_bits, 
        const Bitset& l_relevant = 0)const
    {
        return(l_relevant ? 
            ((l_bits.GetMask() & l_relevant) == (bits & l_relevant))
            : (l_bits.GetMask() == bits));
    }

    bool GetBit(const unsigned int& l_pos)const{
        return ((bits&(1 << l_pos)) != 0);
    }
    void TurnOnBit(const unsigned int& l_pos){
        bits |= 1 << l_pos;
    }
    void TurnOnBits(const Bitset& l_bits){
        bits |= l_bits;
    }
    void ClearBit(const unsigned int& l_pos){
        bits &= ~(1 << l_pos);
    }
    void ToggleBit(const unsigned int& l_pos){
        bits ^= 1 << l_pos;
    }

    void Clear(){ bits = 0; }
private:
    Bitset bits;
};

当我尝试下面的代码时,我得到了这个错误using EntityContainer = std::unordered_map;:

'operator __surrogate_func': 找不到匹配的重载函数

struct pair_hash {
    template <class T1, class T2>
    std::size_t operator () ( const std::pair<T1, T2>& p ) const {
        auto h1 = std::hash<T1>{}( p.first );
        auto h2 = std::hash<T2>{}( p.second );

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

using EntityId = unsigned int;

using ComponentContainer = std::vector<C_Base*>;
using EntityData = std::pair<Bitmask,ComponentContainer>;

// ERROR below at line:
using EntityContainer = std::unordered_map<EntityId, EntityData, pair_hash>;

【问题讨论】:

  • 我们可以得到minimal reproducible example 吗? std::pair 确实定义了 operator ==
  • 另外,请编辑您的问题以逐字显示完整的错误消息并标记生成错误的代码行。
  • std::unordered_map 只需要对密钥进行哈希处理/具有operator==。所以EntityContainer 应该没问题。 Component 是什么(作为ComponentFactory 的密钥)?
  • 请注意,虽然std::pair 至少定义了operator== 的一个组件std::pair -- Bitmask -- 没有。
  • 对不起,我的项目很大,小项目我没有,我添加信息错误行,你可以看到它。

标签: c++ c++17 sfml unordered-map std-pair


【解决方案1】:

虽然std::pair 实现了operator==,但它依赖于pair 的两个组成部分中的每一个,它们都有自己的可行operator== 定义(直到c++20 提供synthesized_three-way_comparison)。

所以你需要为Bitmask定义一个相等运算符...

class Bitmask {
public:
  ...
  bool operator== (const Bitmask &other) const
    {
      return bits == other.bits;
    }
};

【讨论】:

  • thansk, but so error: >**binary '==': 'std::pair' 未定义此运算符或转换为预定义可接受的类型运营商**
猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多