g ++文字列remove_ifエラー
-
27-10-2019 - |
質問
これがコードです:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string word="";
getline(cin,word);
word.erase(remove_if(word.begin(), word.end(), isspace), word.end());
word.erase(remove_if(word.begin(), word.end(), ispunct), word.end());
word.erase(remove_if(word.begin(), word.end(), isdigit), word.end());
}
2010年VSでコンパイルされたとき、それは完全に正常に機能します。 g ++で編集しています。
hw4pr3.cpp: In function `int main()':
hw4pr3.cpp:20: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
hw4pr3.cpp:21: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
hw4pr3.cpp:22: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
解決
追加 ::
の始まりまで isspace
, ispunct
と isdigit
, 、コンパイラが使用するものを決定できない過負荷があるため、
word.erase(remove_if(word.begin(), word.end(), ::isspace), word.end());
word.erase(remove_if(word.begin(), word.end(), ::ispunct), word.end());
word.erase(remove_if(word.begin(), word.end(), ::isdigit), word.end());
他のヒント
追加 #include <cctype>
(そして、言います std::isspace
あなたがそうでないなら abusing namespace std;
).
必要なすべてのヘッダーを常に含め、隠されたネストされた包含物に依存しないでください。
また、他の人からの過負荷を明らかにしなければならないかもしれません <locale>
. 。明示的なキャストを追加してこれを行います。
word.erase(std::remove_if(word.begin(), word.end(),
static_cast<int(&)(int)>(std::isspace)),
word.end());
私にとっては、以下のいずれかを行う場合、G ++を使用してコンパイルします。
- 削除する
using namespace std;
そして変化しますstring
にstd::string
;また - 変化する
isspace
に::isspace
(等。)。
これらのいずれかが引き起こします isspace
(など)メインネームスペースから取られる可能性があると解釈されるのではなく、 std::isspace
(等。)。
問題は、STD :: ISSPACE(INT)をパラメーターとして採用しているが、文字列はcharで構成されていることです。したがって、あなたはあなた自身の機能を次のように書く必要があります:
bool isspace(char c){return c == ''; }
同じことが他の2つの関数にも当てはまります。
所属していません StackOverflow