簡體   English   中英

如何將 std::unordered_map 與 std::pair 一起使用?

[英]How to Use std::unordered_map with std::pair?

我有這個錯誤:

二進制 '==': 'std::pair<const EntityId,EntityData>' 未定義此運算符或轉換為預定義運算符可接受的類型*

在這段代碼中:

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 class:

#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 class:

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

當我嘗試下面的代碼時,我使用 EntityContainer = std::unordered_map;得到這個錯誤

'operator __surrogate_func':找不到匹配的重載 function

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

盡管std::pair實現了operator== ,但它依賴於該pair的兩個組成部分中的每一個都具有自己的可行operator==定義(直到 c++20 提供了synthesis_three-way_comparison )。

所以你需要為Bitmask定義一個相等運算符...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM