C++ STL地图<;字符串,字符串>;,将0值赋给键会导致编译错误
我在使用map类时遇到了一个编译器问题,并编写了以下简单程序来突出显示错误:C++ STL地图<;字符串,字符串>;,将0值赋给键会导致编译错误,c++,string,c++11,stl,C++,String,C++11,Stl,我在使用map类时遇到了一个编译器问题,并编写了以下简单程序来突出显示错误: 1 #include <string> 2 #include <map> 3 4 using namespace std; 5 6 int main() 7 { 8 map<string, string> testmap; 9 10 11 testmap["one"] = 11; 12 testmap["two"] =
1 #include <string>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<string, string> testmap;
9
10
11 testmap["one"] = 11;
12 testmap["two"] = 22;
13 testmap["zero"] = 0;
14 // testmap["zero"] = 10;
15
16 return 0;
17 }
1#包括
2#包括
3.
4使用名称空间标准;
5.
6 int main()
7 {
8地图测试图;
9
10
11测试图[“一”]=11;
12测试图[“两个”]=22;
13 testmap[“零”]=0;
14//testmap[“零”]=10;
15
16返回0;
17 }
我发现以下编译错误:
g++./test.cc
./test.cc:在函数“int main()”中:
./test.cc:13:23:错误:testmap.std::map::operator[],std::basic_string,std::less>,>std::allocator,std::basic_string>>(*&std::basic_string(((const char*)“zero”),((const std::allocator)(&std::allocator())=0'
/测试。抄送:13:23:注:考生为:
在/usr/include/c++/4.7/string:54:0中包含的文件中,
from./test.cc:1:
/usr/include/c++/4.7/bits/basic_string.h:543:7:注:std::basic_string&std::basic_string::operator=(const std::basic_string&)[带>\u CharT=CharT;\u Traits=std::char_Traits;\u Alloc=std::分配器;std::basic_string=std::basic_string=std::basic_string]
/usr/include/c++/4.7/bits/basic_string.h:551:7:注:std::basic_string&std::basic_string::operator=(const_CharT*)[with _CharT=char;_Traits=>std::char_Traits;_Alloc=std::分配器;std::basic_string=std::basic_string=std::basic_string]
/usr/include/c++/4.7/bits/basic_string.h:562:7:注:std::basic_string&std::basic_string::operator=(_CharT)[with _CharT=char;_Traits=std::char_Traits;_Alloc=std::allocator;std::basic_string=std::basic_string=std::basic_string]
我有几个问题:
1.起初,我认为映射“值”-11和22正在转换为字符串。然而,在得到这个编译器错误后,我相信不是这样。发动机罩下面到底发生了什么:
testmap[“一”]=11
他们不是。您的11和12个作业匹配
basic_字符串和运算符=(图表ch)代码>-所以它将数字11和12视为字符常量-可能不是您想要的。假设您将它们发送到终端或打印机alastd::cout,您可以为字符串指定字符,也可以指定指针。0符合这两个条件。