简体   繁体   English

使用数据类型(类类型)作为映射中的键

[英]Use data type (class type) as key in a map

I have class Base and classes Derived_1 , Derived_2 ... I need derived classes to have an id. 我有类Base和类Derived_1Derived_2 ......我需要派生类来获取id。 Those ids are used for further lookups etc, and thus need to be consecutive (no just some random numbers). 那些id用于进一步查找等,因此需要是连续的(不只是一些随机数)。 Because derived classes are created by user, id can not be member of Derived_N . 由于派生类是由用户创建的,因此id不能是Derived_N成员。 So I came up with DerivedType class. 所以我提出了DerivedType类。

class DerivedType
{
    static unsigned id;
    unsigned m_id;
public:
    DerivedType() : m_id(id++) {  }
}

Now I want to create a mapping between Derived_N and DerivedType . 现在我想在Derived_NDerivedType之间创建一个映射。 Whenever Derived_N is created, this mapping looks if DerivedType for particular Derived_N already exist and returns it, otherwise create new and stores in the map. 每当创建Derived_N ,此映射DerivedType查看特定Derived_N已存在并返回它,否则在地图中创建new和store。

Actual question: Is there any way to use std::map with data type as key in the map? 实际问题:有没有办法在std::map使用带数据类型的 std::map作为 I am not afraid of any template-metaprogram solution. 我不怕任何模板 - 元程序解决方案。 Or is there elegant way how to achieve my goal? 或者有优雅的方式如何实现我的目标?

edit Date type -> Data type, I mean like ClassType, I am sorry :) 编辑日期类型 - >数据类型,我的意思是像ClassType,对不起:)

I want to use it like: 我想用它像:

Derived_5 d;
DerivedType dt = getType(d); //Derived_5 is looked up in map, returning particular DerivedType
dt.getId();

every instance of Derived_N (with same 'N') should have the same id throu DerivedType Derived_N每个实例(具有相同的'N')应该具有与DerivedType相同的id

EDIT2 - MY ANSWER I found better solution for my problem... It is like this: EDIT2 - 我的答案我找到了解决问题的更好方法......就像这样:

atomic_counter s_nextEventClassID;

typedef int cid_t;

template<class EventClass>
class EventClassID
{
public:
    static cid_t getID()
    {
        static cid_t classID = EventClassID::next();
        return classID;
    }

    static cid_t next() { return ++s_nextEventClassID; }
};

since my question was how to use datatype in a map, I will mark some of yours answers, thank you 既然我的问题是如何在地图中使用数据类型,我会标记你的一些答案,谢谢

C++11 solves this by providing std::type_index , in <typeindex> , which is a copyable, comparable and hashable object constructed from a std::type_info object that can be used as the key in associative containers. C ++ 11通过在<typeindex>提供std::type_index来解决这个问题,它是一个可复制的,可比较的,可散列的对象,由std::type_info对象构造,可以用作关联容器中的键。

(The implementation is fairly simple, so even if you don't have C++11 yourself, you could steal the implementation from, say GCC 4.7, and use that in your own code.) (实现相当简单,所以即使你自己没有C ++ 11,也可以从GCC 4.7中窃取实现,并在你自己的代码中使用它。)

#include <typeindex>
#include <typeinfo>
#include <unordered_map>

typedef std::unordered_map<std::type_index, int> tmap;

int main()
{
    tmap m;
    m[typeid(main)] = 12;
    m[typeid(tmap)] = 15;
}

You can use typeid(object) directly, since there is type_info::before , which can be used as comparator if you use type_info as key in the map, see What is `type_info::before` useful for? 您可以直接使用typeid(object) ,因为有type_info::before ,如果在地图中使用type_info作为键,可以用作比较器,请参阅什么是`type_info :: before`有用? . No need to get .name() . 无需获取.name()

You can use whatever type or class you want as the key to std::map , provided you give the template arguments a compare function that tells it how to sort the underlying tree. 您可以使用任何类型或类作为std::map的键,前提是您为模板参数提供了一个比较函数,该函数告诉它如何对基础树进行排序。

The easiest thing to do IMHO to represent dates as keys, is to convert them to unix timestamps, but no matter what the class representation of them may be, just provide a comparison function to the map's definition and you're good to go. 使用IMHO将日期表示为键的最简单方法是将它们转换为unix时间戳,但无论它们的类表示是什么,只需为地图的定义提供比较函数,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM