问题
I've been looking boost::tokenizer, and I've found that the documentation is very thin. Is it possible to make it tokenize a string such as "dolphin--monkey--baboon" and make every word a token, as well as every double dash a token? From the examples I've only seen single character delimiters being allowed. Is the library not advanced enough for more complicated delimiters?
回答1:
It looks like you will need to write your own TokenizerFunction to do what you want.
回答2:
using iter_split allows you to use multiple character tokens.
The code below would produce the following:
dolphin
mon-key
baboon
#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'; ;
}
回答3:
I know the theme is quite old, but it is shown in the top links in google when I search "boost tokenizer by string"
so I will add my variant of TokenizerFunction, just in case:
class FindStrTFunc
{
public:
FindStrTFunc() : m_str(g_dataSeparator)
{
}
bool operator()(std::string::const_iterator& next,
const std::string::const_iterator& end, std::string& tok) const
{
if (next == end)
{
return false;
}
const std::string::const_iterator foundToken =
std::search(next, end, m_str.begin(), m_str.end());
tok.assign(next, foundToken);
next = (foundToken == end) ? end : foundToken + m_str.size();
return true;
}
void reset()
{
}
private:
std::string m_str;
};
after we can create
boost::tokenizer<FindStrTFunc> tok("some input...some other input");
and use, like a usual boost tokenizer
回答4:
One option is to try boost::regex. Not sure of the performance compared to a custom tokenizer.
std::string s = "dolphin--monkey--baboon";
boost::regex re("[a-z|A-Z]+|--");
boost::sregex_token_iterator iter(s.begin(), s.end() , re, 0);
boost::sregex_token_iterator end_iter;
while(iter != end_iter)
{
std::cout << *iter << '\n';
++iter;
}
来源:https://stackoverflow.com/questions/1252224/using-boosttokenizer-with-string-delimiters