c++ - STL map<string, string>,给key赋0值导致编译错误

标签 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"] = 22;
 13     testmap["zero"] = 0;
 14     // testmap["zero"] = 10;
 15
 16     return 0;
 17 }

我得到以下编译错误:

g++ ./test.cc ./test.cc: In function 'int main()': ./test.cc:13:23: error: ambiguous overload for 'operator=' in 'testmap.std::map<_Key, _Tp, _Compare, _Alloc>::operator[], std::basic_string, std::less >, >std::allocator, std::basic_string > > >((* & std::basic_string(((const char*)"zero"), ((const std::allocator)(& std::allocator()))))) = 0' ./test.cc:13:23: note: candidates are: In file included from /usr/include/c++/4.7/string:54:0, from ./test.cc:1: /usr/include/c++/4.7/bits/basic_string.h:543:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with >_CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string] /usr/include/c++/4.7/bits/basic_string.h:551:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = >std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string] /usr/include/c++/4.7/bits/basic_string.h:562:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string]

我有几个问题: 1. 最初我认为 map “值”- 11 和 22 正在转换为字符串。然而,在得到这个编译器错误之后让我相信不是这样。下面到底发生了什么: 测试图[“一”] = 11;

  1. 如果值 11、22 实际上已转换为字符串,那么为什么 0 没有转换为字符串。

  2. 我正在努力理解编译器错误消息,因此我可以自己解码错误。我理解它的一部分,其中模板映射类定义被扩展为字符串类型的键/值。谁能指出错误消息中可能有助于我理解问题的部分。

非常感谢任何帮助。

谢谢, 艾哈迈德。

最佳答案

If the values 11, 22 is in fact converted to string, then why is 0 not converted to a string.

他们不是。您的 11 和 12 作业匹配 basic_string& operator=( CharT ch ); - 所以它将数字 11 和 12 视为字符常量 - 可能不是您想要的。假设您将它们发送到终端或打印机 ala std::cout << testmap["one"]; - 11 对应于“垂直制表符”控制代码,可能会留下一些空白行,而 12 对应于“换页”,可能会使打印页面的其余部分留空或清除屏幕(请参阅 http://en.wikipedia.org/wiki/ASCII)。

0在允许隐式转换为指针的意义上是特殊的(在 C++03 中 NULL 将被定义为 0 ),因此转换是不明确的(即 charconst char* ?) - 参见 http://en.cppreference.com/w/cpp/string/basic_string/operator%3D查看 std::string operator= 的列表

4.10 Pointer conversions [conv.ptr] 1 A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; ...

其他整数没有这种指针转换分配 - 这就是为什么您不会收到它们的编译器错误。

必须:

  • 正确地将您的数字转换为字符串表示形式,例如使用 std::to_string() , boost::lexical_cast , 一个 std::ostringstream等等,或者
  • 使用std::map<string, int>或类似的。

关于c++ - STL map<string, string>,给key赋0值导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20754161/

相关文章:

c++ - 为什么 std::exception 在 std::bad_alloc 之前捕获我的异常?

python - 如何找到所有出现的子字符串?

javascript - Google Apps 脚本错误值

c++ - 错误 : cannot specify explicit initializer for array

c++ - 如何使用此 OpenMP 关键部分避免此原始指针?

c++ - 线程 vs 基于任务 vs 异步编程

c++ - 尝试在C++中使用curl时出现未解析的符号

c++ - 自定义字符串类中的堆损坏

c++ - 用不同的值初始化 const 容器的正确方法是什么?

c++ - 如何使用 unique_ptr 将 const 指针传递给 const 对象