开发者

c++ map with same key type and different item type [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

how do you make a heterogeneous boost:开发者_StackOverflow中文版:map?

It's possible have a map in C + + with the same type for the key and different types of items? For example:

 _______________________
 |    key    |   value   |
 |===========|===========|
 | string    |   int     |
 |-----------|-----------|
 | string    |   char    |
 |-----------|-----------|
 | string    |  vector   |
 |-----------|-----------|
 | string    |   ....    |


Yes you can, store a variadic type, such as boost::any or (my personal preference, boost::variant)

So your value type can be defined as:

typedef boost::variant<int, char, ...> value_type;

Store than in the map, and then once you've extracted values, use the visitor concept to process.


I'm not sure quite why you would want to do this, but you could use a map<std::string, boost::any> I believe.

What are you really trying to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜