我对 boost tokenizer 有疑问,这是我的代码:
#include <iostream>
#include <vector>
#include <boost/tokenizer.hpp>
using namespace std;
static vector<std::string> tokenize(const std::string& input, const char delim) {
std::cout << "Tokenize: " << input << std::endl;
vector<std::string> vector;
typedef boost::char_separator<char> TokenizerSeparator;
typedef boost::tokenizer<TokenizerSeparator> Tokenizer;
TokenizerSeparator separator(&delim);
Tokenizer tokenizer(input, separator);
Tokenizer::iterator iterator;
for(iterator=tokenizer.begin(); iterator!=tokenizer.end();++iterator){
std::cout << "elem found: " + *iterator << std::endl;
vector.push_back(*iterator);
}
return vector;
}
int main(int argc, const char * argv[])
{
string input = "somedata,somedata,somedata-somedata;more data;more data";
vector<string> list = tokenize(input, ';');
return 0;
}
此代码的行为并非始终一致。有时它有效,有时在多次运行时无效。当它在这里不起作用时,我得到一个输出:
Tokenize: somedata,somedata,somedata-somedata;more data;more data
elem found: some
elem found: ata,some
elem found: ata,some
elem found: ata-some
elem found: ata
elem found: more
elem found: ata
elem found: more
elem found: ata
我究竟做错了什么 ?
谢谢。