DarkPrince
DarkPrince

Reputation: 13

How to store more than 2 values in multimap?

im stuck at a problem, How do we store 3 values through multimap. For example one int and 2 c++ strings. How do we store those 3 values. I tried making a struct and storing 2 strings and then passing it in multimap code as

struct names{ std::string name; std::string secondname; }; and done with

multimap<int, names>Multimap;

first take number from user, second take name from user, third take name form user and then

Multimap.insert(make_pair(number,{name,secondname}));

Upvotes: 1

Views: 652

Answers (2)

Shubham kumar
Shubham kumar

Reputation: 812

Please define type of pair too like this:

struct names{ std::string name; std::string secondname; }; 
multimap<int, names>Multimap;

Multimap.insert(make_pair<int, names>(number,{"name","secondname"}) );
// or simply you can use pair() too instead of make_pair()

Upvotes: 1

I S
I S

Reputation: 456

Another way is you can use tuples.

multimap<int, std::tuple<std::string, std::string>> Multimap; 
Multimap.insert(make_pair(number, std::make_tuple(name, secondname));

https://en.cppreference.com/w/cpp/utility/tuple

Upvotes: 0

Related Questions