问题
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}};
回答1:
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
回答2:
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}}
};
来源:https://stackoverflow.com/questions/28796290/how-to-store-more-than-2-variables-in-an-unordered-map