c++ - 使用带有 split_iterator 的分类器

标签 c++ boost

关于 boost 字符串算法,我似乎遗漏了一些东西。我正在尝试将 split_iterator 与使用 Clasifier 作为拆分点一起使用。所以,例如,我希望能够做这样的事情:

make_split_iterator(str, is_space);

但是 make_split_iterator 需要一个 Range 和一个 Finder。所以我需要的是找到一些序列来从分类器创建查找器。有谁知道如何做到这一点,或者甚至可能吗?

最佳答案

您可以使用 token_finder ,如 make_split_iterator(str, token_finder(is_space()))make_split_iterator(str, token_finder(is_any_of("")))

完整示例,假设您的目标是标记化(因此 token_compress_on)

#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>
int main()
{
        std::string str = "This is a      test string";

        for( boost::algorithm::split_iterator<std::string::iterator> i
            = make_split_iterator(str, token_finder(
                                        boost::algorithm::is_space(),
                                        boost::algorithm::token_compress_on));
             i != boost::algorithm::split_iterator<std::string::iterator>();
             ++i)
        {
                std::cout << *i << '\n';
        }
}

试运行:https://ideone.com/vQ2ZM

关于c++ - 使用带有 split_iterator 的分类器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5495032/

相关文章:

c++ - 使用 boost::ref 来指示编码约定中的意图

c++ - Boost Property Tree Json 读取包含 LPWSTR 的文件

c++ - 以秒为单位获取 boost::posix_time::time_duration

c++ - 解析 linux 内核文件 "Idle Service Error semantic-idle-summary-idle-function - Arithmetic error"时出现 cedet-语义错误 "jiffies.h"

python - 从 C++ 检索 Python 类型

c++ - 从 cin 中读取一个整数序列并将它们存储在一个 vector 中

c++ - 当通过 IDispatch 使用错误的参数数量/类型调用方法时会发生什么

c++ - cmake 在添加的可执行文件中使用库目标

c++ - 我们可以将 char[undetermined_during_compile_time_size] 保留在 boost::shared_ptr 中吗?

c++ - Boost库用元组简单编译