c++ - 使用带有字符串定界符的 boost::tokenizer

标签 c++ string boost tokenize

我一直在寻找 boost::tokenizer,我发现文档非常薄。是否可以让它标记一个字符串,例如“dolphin--monkey--baboon”,并使每个单词成为标记,以及每个双破折号成为标记?从示例中我只看到允许使用单个字符定界符。对于更复杂的定界符,库是否不够先进?

最佳答案

使用 iter_split 允许您使用多个字符标记。 下面的代码将产生以下内容:

海豚
猴子
狒狒

#include <iostream>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/iter_find.hpp>

    // code starts here
    std::string s = "dolphin--mon-key--baboon";
    std::list<std::string> stringList;
    boost::iter_split(stringList, s, boost::first_finder("--"));

    BOOST_FOREACH(std::string token, stringList)
    {    
        std::cout << token << '\n';  ;
    }

关于c++ - 使用带有字符串定界符的 boost::tokenizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46862353/

相关文章:

c++ - boost::asio async_read_some 工作一次然后停止工作,为什么? (使用 shared_ptr)

c++ - 使用链表堆叠

c# - 如何在 C# 中制作通用数字解析器?

c++ - C/C++ strcmp 无法将参数 1 从 'char' 转换为 'const char *'

regex - 解析字符串并在perl中执行操作

string - Go:理解字符串

c++ - 线程 + boost::function 中的 getline 访问冲突

c++ - Boost 在序列化过程中丢失了一些数据

c++ - SFINAE 适用的 C++11 标准中提到的 "immediate context"到底是什么?

c++ - 如何初始化双**?