gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:53:02 27 4
gpt4 key购买 nike

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

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

这是我尝试过的:

#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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com