我一直在寻找 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/