I currently am using a map in C++ and have a couple of values that are ints and booleans, though the majority of them are strings. I know in Java I could do something like this:
I don't know of a way you could have a std::map
with keys that can be multiple types using the stdlib
out of the box.
Boost has a variant type that functions more or less like what you want.
typedef boost::variant key_type;
std::map mapvar
This map will only accept keys that are defined in your chosen key_type, so you could not also use, say, a std::wstring
as a key without explicitly declaring it. This would be the advantage of using a variant
over any
. The latter might happily eat whatever type you send its way, while the former will give you a compile time error.
If you are going to use a boost::variant
as your key then you need to read the boost documentation and look into what is involved in constructing variant
objects and whether or not they have sorting pre-defined. std::map
is built on operator<
(unless you template it on some other sorting functor) so you'll need to make sure a working definition for your variant
type is provided by either the library or yourself.
--
Why do you want to have such a map in the first place?