简体   繁体   English

如何将 MAP 中的键值成对存储在 C++ 中

[英]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我正在尝试解决一个名为(属性解析器)的黑客等级问题: 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;所以我想出了一个解决方案,我可以使用map但为了解决它,我需要插入两个键 1 是对,另一个是字符串map<pair<string,string>,string>DATA;

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

But it's giving an error:但它给出了一个错误:

no matching member function for call to 'emplace'没有匹配的成员 function 用于调用“emplace”

Now how to insert elements as well as access them?现在如何插入元素以及访问它们?

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.没有从{Type,Tag}std::pair<string,string>的隐式转换,即使它们是string类型。 Rather use std::make_pair而是使用std::make_pair

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

Instead of doing DATA.emplace({Type, Tag},Data);而不是做 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM