述語関数の署名が直接要素をチェックすることになっているので、あなたは、std::remove_if
とラムダのパラメータの型としてchar
を取る必要があります。
auto detect_bracket = [](char x){ return(')' == x || '(' == x);};
this->str.erase(std::remove_if(str.begin(), str.end(),
detect_bracket)
);
注std::string::back()
はstd::remove_if
では動作しません。 char
を返し、std::remove_if
はイテレータで表される範囲を返します。あなただけの開始と終了、あなたは可能性が
auto detect_bracket = [](char x){ return(')' == x || '(' == x);};
if (!this->str.empty()) {
this->str.erase(std::remove_if(str.begin(), str.begin() + 1, detect_bracket), str.begin() + 1);
}
if (!this->str.empty()) {
this->str.erase(std::remove_if(str.end() - 1, str.end(), detect_bracket), str.end());
}
でstd::remove_if
が返されますので、我々は、std::string::erase
の正しい終了イテレータを指定する必要があります注要素を削除したい場合は
そしてstr.begin(), str.begin()
は、単に空の範囲であります何も見つからなかったとしてもイテレータは、charは間違って消去されます。
template< class ForwardIt, class UnaryPredicate >
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p);
p
- unary predicate which returns true
if the element should be removed. The signature of the predicate function should be equivalent to the following:
bool pred(const Type &a);
The type Type
must be such that an object of type ForwardIt
can be dereferenced and then implicitly converted to Type.
必要なのはchar*
からchar
にあなたの関数のパラメータを変更することです:
LIVE
'[](char x){return( ')' == x || '(' == x);} 'で十分ではありませんか? – 101010
@ 101010 '/usr/include/c++/4.8/bits/stl_algo.h: '_FIter std :: remove_if(_FIter、_FIter、_Predicate)のインスタンス化では、[_FIter = char; _Predicate = ExprContainer :: removeBrackets():: __ lambda0] ': ../src/CMEXPR.cpp:194:20:ここから必要 /usr/include/c++/4.8/bits/stl_algo.h:1150:25 :エラー:unary '*'( 'char'を持つ)の無効な型引数 ' –
は' char * 'ではなく' char'です。 – 101010