我最近被这个震惊了:
#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;
}
Run Code Online (Sandbox Code Playgroud)
它打印'X',即ASCII为88.
为什么字符串映射接受int作为值?map的文档operator[]说它返回的mapped_type&是string&这种情况,它没有隐式转换int,是吗?