Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store more than 2 variables in an unordered_map?

How can I store more than 2 variables in an std::unordered_map?

I want something like this:

std::unordered_map<string, int, int, int> mapss = {{"a",1,1,1},{"b",1,2,3}};
like image 362
kishoredbn Avatar asked Dec 06 '25 03:12

kishoredbn


2 Answers

If the string is the key, and the rest are values, then you could have the value be a tuple.

unordered_map<string, tuple<int, int, int>> mapss

Of if you don't know how many values will be there, you can use a vector

unordered_map<string, vector<int>> mapss
like image 128
Cory Kramer Avatar answered Dec 09 '25 04:12

Cory Kramer


You can use an std::tuple, as Cyber mentioned, but I suggest creating a simple struct if you know what the values represent.

It expresses your intent clearly.

Example:

struct Color
{
    int r, g, b;
};

std::unordered_map<std::string, Color> colors = 
{
    {"red",  {255, 0, 0}},
    {"blue", {0, 0, 255}}
};
like image 42
Vittorio Romeo Avatar answered Dec 09 '25 04:12

Vittorio Romeo