Boostライブラリで文字列から半角・全角スペースを削除する
Boost String Algorithms ライブラリを使って指定した文字を削除する。
サンプルは視認しやすくするため、全角スペースをアンダースコアに置換しています。
#include <iostream> #include <boost/algorithm/string.hpp> using namespace std; using namespace boost; int main() { string s1 = " _ _ "; // 半角・全角スペースのみを含む文字列 string s2 = " s t_r_"; string set = " _"; // 半角・全角スペースのみで構成される文字列を判定 cout << all(s1, is_any_of(set)) << endl; // true cout << all(s2, is_any_of(set)) << endl; // false // 半角・全角スペースを文字列から削除する find_format_all(s2, token_finder(is_any_of(set)), empty_formatter("")); cout << s2 << endl; // str }