What is the best way to map two values to one key?
ie An item with a value and bool.
Tried using:
std::map<std::string, std::pair<std::string, bool> > myMap2
But that doesn't seem like the correct solution. Is there a more elegant way to do this?
That is indeed the correct solution. More generally, consider using
std::tuple
instead ofstd::pair
for a uniform interface regardless of the number of values (asstd::pair
is obviously restricted to two), orboost::tuple
if your compiler is too old to ship with astd::
orstd::tr1::
implementation.Normally, I crate a simple mapValue struct/class.
Either use
std::pair<>
as you did, or make a custom struct containing the values you want to store. I'd do the latter in most cases, as the values then have names more descriptive thanfirst
andsecond
.