c++ map with same key type and different item type [duplicate]
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?
精彩评论