boost::erase_all 从字符串中删除多个字符
boost::erase_all to erase multiple chars from a string
如果我想使用 boost::erase_all
从字符串中删除所有 ,我可以这样做:
boost::erase_all( "a1b1c1", "1" );
现在,我的字符串是"abc"。但是,如果我想从字符串中删除所有数字 (0 - 9),使用 boost::erase_all
,我必须为要删除的每个数字调用它一次。
boost::erase_all( "a1b2c3", "1" );
boost::erase_all( "a1b2c3", "2" );
boost::erase_all( "a1b2c3", "3" );
我以为我可以使用 boost::is_any_of
一次将它们全部删除,因为这适用于其他 boost 字符串算法,例如 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>&)’
也许我在这里忽略了一些明显的东西,或者 boost 中还有另一个功能可以做到这一点。我可以使用标准C++手动完成此操作,但只是好奇其他人如何使用 boost 来执行此操作。
感谢您的任何建议。
现在还提供:
boost::remove_erase_if(str, boost::is_any_of("123"));
boost 有一个 remove_if 版本,不需要你传入 begin 和 end 迭代器,但你仍然需要使用 end 迭代器对字符串调用 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
相关文章:
- 正在从C++中的字符数组中删除字符
- 从字符串中删除字符
- 如果不是十六进制,则QT从qlineedit中删除字符
- 从长字符串中删除字符
- 从文件对象的当前位置删除字符
- C++ 删除字符**
- 从字符串中删除字符的第一个和最后一个实例
- 如何在C++中删除字符*数组
- 如何从C 中的字符串中删除字符
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- 删除字符并在字符串(C )的末端附加它们
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- 在字符串中的定界符之间删除字符
- 从 std::字符串中删除字符从 "(" 到 ")" 用擦除?
- 使用擦除和删除从字符串中删除字符
- C++ 如何删除字符中的双引号
- C++ 中的密码 |打印字符后删除字符
- C++在链表中插入和删除字符
- 字符串流的获取函数不删除字符
- 删除字符C++最后一次出现时字符数组的开头