C ++ 11 The simplest way to store multiple data types for a value (int and string) on ​​a map <key, value>?

I want to have a card that uses

  • key string
  • int OR string for value

like this:

std::map<std::string, SOME TYPE> myMap; myMap["first_key"] = 10; myMap["second_key"] = "stringValue"; 

What is an EASY way to do such a thing?

added) I'm looking for a solution that works in C ++ 11

+5
source share
1 answer

In C ++ 17, you can use std::variant<int, std::string> , before that you can use one of boost :

 using IntOrString = std::variant<int, std::string>; std::map<std::string, IntOrString> myMap; myMap["first_key"] = 10; myMap["second_key"] = "stringValue"; 
+9
source

All Articles