c++ - 如何使用 C++/Boost 从字符串中过滤字符

标签 c++ string boost

这似乎是一个基本问题,所以如果它已经在某处得到回答,我深表歉意(我的搜索没有找到任何结果)。

我只想过滤一个字符串对象,使其只包含字母数字和空格字符。

这是我尝试过的:

#include "boost/algorithm/string/erase.hpp"
#include "boost/algorithm/string/classification.hpp"

std::wstring oldStr = "Bla=bla =&*\nSampleSampleSample ";
std::wstring newStr = boost::erase_all_copy(oldStr, !(boost::is_alnum() || 
                                                      boost::is_space()));

但编译器对此一点也不满意——似乎我只能在 erase_all_copy 的第二个参数中放置一个字符串,而不是这个 is_alnum()东西。

我在这里缺少一些明显的解决方案吗?

最佳答案

使用标准算法和 Boost.Bind:

std::wstring s = ...
std::wstring new_s;
std::locale loc;
std::remove_copy_if(s.begin(), s.end(), std::back_inserter(new_s), 
    !(boost::bind(&std::isalnum<wchar_t>, _1, loc)||
      boost::bind(&std::isspace<wchar_t>, _1, loc)
));

关于c++ - 如何使用 C++/Boost 从字符串中过滤字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3206310/

相关文章:

c++ - 在Boost Graph Library中选择给定顶点的随机进出邻居的有效方法?

c++ - 如何从 boost::thread 中检索线程 ID?

c++ - 如何在 C++ 中成对显示正则表达式输出?

c++ - 如何在 C++ 中创建参数化对象数组?

c++ - 将回调函数打包到模板类中

javascript - 如何用数组的值替换字符串中的问号?

c++ - 在 Managed_shared_memory 中 boost 进程间互斥

c++ - 使用 MPFR 计算不同精度的次正规数

php - 有没有办法在 PHP 双引号字符串中使用函数?

java - if 语句没有采用我的字符串值并将它们评估为 boolean 值(java初学者问题)