C++ map<string, string> 如何在这张地图中插入数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3709031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
map<string, string> how to insert data in this map?
提问by bjskishore123
I need to store strings in key value format. So am using Map like below.
我需要以键值格式存储字符串。所以我使用如下地图。
#include<map>
using namespace std;
int main()
{
map<string, string> m;
string s1 = "1";
string v1 = "A";
m.insert(pair<string, string>(s1, v1)); //Error
}
Am getting below error at insert line
我在插入行低于错误
error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
错误 C2784:'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)':无法推导出 'const std::_Tree<_Traits> &' 的模板参数来自'const std::string'
I tried make_pair function also like below, but that too reports the same error.
我也像下面一样尝试了 make_pair 函数,但它也报告了同样的错误。
m.insert(make_pair(s1, v1));
Pls let me know what's wrong and what's the solution for above problem. After solving above problem, can i use like below to retrieve value based on key
请让我知道出了什么问题以及上述问题的解决方案是什么。解决上述问题后,我可以使用下面的方法根据键检索值吗
m.find(s1);
回答by Etienne de Martel
I think you miss a #include <string>
somewhere.
我想你错过了#include <string>
某个地方。
回答by Daren Thomas
Could you try this:
你可以试试这个:
#include<string>
It seems the compiler doesn't know how to compare strings. Maybe she doesn't know enough about strings yet, but is too focused on your map
to figure that out ATM.
编译器似乎不知道如何比较字符串。也许她对字符串的了解还不够多,但太专注于你而map
无法弄清楚 ATM。
回答by tdammers
Try m[s1] = v1;
instead.
试试吧m[s1] = v1;
。
回答by TadejP
You have several possibilities how store strings in key value format now:
您现在有几种可能如何以键值格式存储字符串:
m["key1"] = "val1";
m.insert(pair<string,string>("key2", "val2"));
m.insert({"key3", "val3"}); // c++11
And traverse it in c++11:
并在 c++11 中遍历它:
for( auto it = m.begin(); it != m.end(); ++it )
{
cout << it->first; // key
string& value = it->second;
cout << ":" << value << endl;
}
回答by Alexey Tkach
Here is the way to set up map<...,...>
这是设置 map<...,...> 的方法
static std::map<std::string, RequestTypes> requestTypesMap = {
{ "order", RequestTypes::ORDER },
{ "subscribe", RequestTypes::SUBSCRIBE },
{ "unsubscribe", RequestTypes::UNSUBSCRIBE }
};
回答by Default
I think it has to do with the fact that <map>
doesn't include <string>
, but <xstring>
. When you are adding elements to the map it needs to find the correct position in the map by sorting. When sorting, map tries to locate the operator <
, from which it finds the correct location for the new element. However, there is no operator <
for the definition of string in <xstring>
, thus you get the error message.
我认为这与<map>
不包括<string>
, but的事实有关<xstring>
。当您向地图添加元素时,它需要通过排序在地图中找到正确的位置。排序时,map 尝试定位operator <
,从中找到新元素的正确位置。但是,operator <
字符串中的定义没有<xstring>
,因此您会收到错误消息。
回答by badrequest
The s1 is an integer you are hoping pass as string...thats likely the main cause of the error!!
s1 是一个整数,你希望作为字符串传递......这可能是错误的主要原因!!