Atif Iqbal
Atif Iqbal

Reputation: 5

How To Store Key Value In MAP As Pair In C++

I'm trying to solve a question on hacker rank named (Attribute Parser) : QUESTION

So I came up with a solution that I could use map but in order to solve it I need to insert two keys 1 would be pair and the other would be a string map<pair<string,string>,string>DATA;

I tried DATA.emplace({Type, Tag},Data);

But it's giving an error:

no matching member function for call to 'emplace'

Now how to insert elements as well as access them?

Upvotes: 0

Views: 1168

Answers (2)

user14148022
user14148022

Reputation:

Instead of doing DATA.emplace({Type, Tag},Data);

//Inserting Element

Try

DATA.emplace(make_pair(Type, Tag),Data);

And To Access It:

//storing pair in variable

pair<string, string> Value(Type, Tag);
cout<<DATA[Value];

Upvotes: 0

Wander3r
Wander3r

Reputation: 1881

DATA.emplace({Type, Tag},Data);

There is no implicit conversion to std::pair<string,string> from {Type,Tag} even if they are of string type. Rather use std::make_pair

DATA.emplace(std::make_pair(Type,Tag), Data);

Upvotes: 3

Related Questions