Sono stato di recente scosso da questo:Perché map <string, string> accetta valori come input?
#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;
}
la stampa 'X', che è 88 in ASCII.
Perché una mappa di stringhe accetta valori come valori? La documentazione per la mappa operator[]
dice che restituisce mapped_type&
che è string&
in questo caso e non ha una conversione implicita da int
, vero?
Correlati: [Perché C++ consente di assegnare un intero a una stringa?] (Http://stackoverflow.com/q/1177704/335858). – dasblinkenlight