Using two objects as hash key for an unordered_map or alternatives

后端 未结 2 1798
不思量自难忘°
不思量自难忘° 2020-12-10 19:13

Having defined my objects myType, I need to store relations between these objects. These relations are stored on a matrix.

The number of elements is not

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 19:51

    You could use a boost::unordered_map with key std::pair in combine with boost::hash. You could declare it as:

    boost::unordered_map, 
                         std::vector>, 
                         boost::hash>> dictionary;
    

    Then you could load the characteristics of each pair in the dictionary like in the example below:

    dictionary[std::make_pair(&a, &b)] = std::vector>(1, {1, 2, 3, 4, 5});
    

    And access them as:

    dictionary[std::make_pair(&a, &b)][0][0];
    

    LIVE DEMO

提交回复
热议问题