C++全然わからん。
#include <iostream> #include <string> #include <map> using namespace std; int main(int argc, char const *argv[]){ string s = "ABC"; map<string, int> mp; mp["A"] = 0; //mp[s[0]]++; // エラー mp[{s[0]}]++; cout << mp["A"] << endl; return 0; }
char型のものは{}で囲めばstring型になってくれるらしい。
int main(int argc, char const *argv[]){ char c = 'c'; string s; s = {c}; s = string({c}); s = (string){c}; //cout << {c} << endl; // コンパイルエラー cout << string({c}) << endl; cout << (string){c} << endl; return 0; }
ただし、上記のようにエラーになる場合があるので、string()で囲むのが無難か。
参考URL: http://marycore.jp/prog/cpp/convert-char-to-string/
PS: C++ってリファレンス読むのしんどい。