【发布时间】:2015-05-29 02:18:27
【问题描述】:
我最近对此感到震惊:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main() {
map<string, string> strings;
strings["key"] = 88; // surprisingly compiles
//map<string, string>::mapped_type s = 88; // doesn't compile as expected
cout << "Value under 'key': '" << strings["key"] << "'" << endl;
return 0;
}
它打印 'X',即 88 ASCII 码。
为什么字符串映射接受整数作为值?地图的operator[] 的文档说它返回mapped_type&,在这种情况下是string&,并且它没有来自int 的隐式转换,是吗?
【问题讨论】:
标签: c++ dictionary