13

を使用して文字列からすべてのものを削除したい場合は、次のboost::erase_allように実行できます。

boost::erase_all( "a1b1c1", "1" );

今、私の文字列は「abc」です。ただし、を使用して文字列からすべての数字(0〜9)を削除する場合は、削除する数字boost::erase_allごとに1回呼び出す必要があります。

boost::erase_all( "a1b2c3", "1" );
boost::erase_all( "a1b2c3", "2" );
boost::erase_all( "a1b2c3", "3" );

boost::is_any_ofなどの他のブースト文字列アルゴリズムで機能するため、一度にすべてを削除できると思いましboost::splitたが、is_any_ofはerase_allでは機能しないようです。

boost::erase_all( "a1b2c3", boost::is_any_of( "123" ) );

// compile error
boost/algorithm/string/erase.hpp:593:13: error: no matching 
function for call to ‘first_finder(const   
boost::algorithm::detail::is_any_ofF<char>&)’

おそらく、私はここで明らかな何かを見落としたか、これを行うことを意図したブースト内の別の関数があります。私は標準のC++で手動でそれを行うことができますが、他の人がこれを行うためにどのようにブーストを使用しているのか興味があります。

アドバイスありがとうございます。

4

2 に答える 2

9

また、現在利用可能です:

boost::remove_erase_if(str, boost::is_any_of("123"));

于 2015-09-13T18:56:02.627 に答える
8

boostにはremove_ifのバージョンがあり、開始イテレータと終了イテレータを渡す必要はありませんが、終了イテレータを使用して文字列に対してeraseを呼び出す必要があります。

#include <boost/range/algorithm/remove_if.hpp>
...
std::string str = "a1b2c3";
str.erase(boost::remove_if(str, ::isdigit), str.end());

http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/algorithms/mutating/remove_if.html

于 2012-05-09T21:04:36.890 に答える