私は<string, int>
というマップを作成しようとしています。私は、キー値のペアのキーとして、指定された文字列の個々の文字を配置しようとしています。しかし、私はこのエラーに実行しています:のペアをchar *と組み合わせてC++の文字列にすることはできません
Line 7: no matching function for call to 'std::unordered_map<std::__cxx11::basic_string<char>, int>::find(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)'
これは私のコードです:
int lengthOfLongestSubstring(string s) {
unordered_map<string, int> map;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (map.find(s[i]) == map.end()) {
count++;
map.insert(make_pair<string, int>(s[i], 1));
} else {
map.clear();
count = 0;
}
}
return count;
}
私は* [i]の文字になりsのため、エラーがあると思うので、私がchar以来make_pairを行うことはできません*とstringは異なる型です。
私が行うことによってこれを回避しようとしている:
string temp(s[i]); // Try to create a string from the char* and pass it into the make_pair function
しかし、私はまだ同じエラーを取得します。
's [i]'は 'char *'ではなく、単に 'char'です。 –
's [i]'は 'char'です。 'std :: string'は' char'だけで構築することはできません。 –
単一の文字の 'map'をキーまたは複数の文字として使いたいですか? –