2017-11-10 7 views
1

私は<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 

しかし、私はまだ同じエラーを取得します。

+2

's [i]'は 'char *'ではなく、単に 'char'です。 –

+2

's [i]'は 'char'です。 'std :: string'は' char'だけで構築することはできません。 –

+1

単一の文字の 'map'をキーまたは複数の文字として使いたいですか? –

答えて

2

I think the error is because s[i] becomes a char* and so I cannot do make_pair since char* and string are different types.

いいえ、what s[i] returns直接std::stringに変換することができないだけで単一char(より正確には、char&参照)、です。 charからstd::stringを構築するために、あなたはすなわち、std::stringの異なるコンストラクタを使用する必要があります。たとえば

basic_string(size_type count, CharT ch, const Allocator& alloc = Allocator())

map.insert(make_pair<string, int>(string(1, s[i]), 1)); 

または:

map.insert(make_pair<string, int>({1, s[i]}, 1)); 
0

std::string::operator[]リターンa(aへの参照)シングルcharchar*ポインタ。 charからstd::stringを作成することはできません。そのため、エラーが発生しています。例えば

basic_string(size_type count, CharT ch, const Allocator& alloc = Allocator()); 

basic_string(const CharT* s, size_type count, const Allocator& alloc = Allocator()); 

charからstd::stringを構築するには、次のコンストラクタのいずれかを使用する必要が

int lengthOfLongestSubstring(string s) { 
    unordered_map<string, int> map; 
    int count = 0; 
    for (int i = 0; i < s.length(); i++) { 
     string key(1, s[i]); // <-- here 
     // or: string key(&s[i], 1); 
     if (map.find(key) == map.end()) { 
      count++; 
      map.insert(make_pair(key, 1)); 
     } else { 
      map.clear(); 
      count = 0; 
     } 
    } 

    return count; 
} 

そうでない場合は、charにごstd::unordered_mapキーを変更します代わりに、あなたが実際に使用しているものなので、

int lengthOfLongestSubstring(string s) { 
    unordered_map<char, int> map; // <-- here 
    int count = 0; 
    for (int i = 0; i < s.length(); i++) { 
     char key = s[i]; 
     if (map.find(key) == map.end()) { 
      count++; 
      map.insert(make_pair(key, 1)); 
     } else { 
      map.clear(); 
      count = 0; 
     } 
    } 

    return count; 
} 

あなたが実際にすべてのマッピングされたpair秒のsecond値を使用していないので、あなたが本当にすべてでstd::unordered_mapを必要としないしかし、それだけで無駄なオーバーヘッドです。代わりにstd::setを使用することを検討してください:

int lengthOfLongestSubstring(string s) { 
    set<char> chars; 
    int count = 0; 
    for (int i = 0; i < s.length(); i++) { 
     // set::insert() return true if inserted, false if duplicate 
     if (chars.insert(s[i]).second) { 
      count++; 
     } else { 
      chars.clear(); 
      count = 0; 
     } 
    } 

    return count; 
} 
関連する問題