Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Simplest way to store multiple data types for value(int and string) in map<key, value>?

Tags:

c++

c++11

I want to have a map that uses

  • string for key
  • int OR string for value

like this:

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

What is the SIMPLEST way to do such thing?

added) I am looking for solution that works in c++11

like image 957
Joon. P Avatar asked Aug 04 '16 12:08

Joon. P


1 Answers

In c++17, you may use std::variant<int, std::string>, prior to this, you may use the one from boost:

using IntOrString = std::variant<int, std::string>;
std::map<std::string, IntOrString> myMap;
myMap["first_key"]  = 10;
myMap["second_key"] = "stringValue";
like image 110
Jarod42 Avatar answered Oct 17 '22 06:10

Jarod42