我最近一直震动本:为什么map <string,string>接受int值作为值?
#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' 这是ASCII 88。
为什么字符串映射接受整数作为值?地图的文档operator[]
表示它返回mapped_type&
这是string&
在这种情况下,它没有从int
隐式转换,是吗?
相关:[为什么C++允许将整数分配给字符串?](http://stackoverflow.com/q/1177704/335858)。 – dasblinkenlight 2015-03-25 12:10:02