【发布时间】:2015-02-05 22:19:54
【问题描述】:
我需要用分隔符标记字符串。
例如:
对于"One, Two Three,,, Four",我需要得到{"One", "Two", "Three", "Four"}。
我正在尝试使用这个解决方案https://stackoverflow.com/a/55680/1034253
std::vector<std::string> strToArray(const std::string &str,
const std::string &delimiters = " ,")
{
boost::char_separator<char> sep(delimiters.c_str());
boost::tokenizer<boost::char_separator<char>> tokens(str.c_str(), sep);
std::vector<std::string> result;
for (const auto &token: tokens) {
result.push_back(token);
}
return result;
}
但我得到了错误:
boost-1_57\boost/tokenizer.hpp(62): 错误 C2228: '.begin' 的左边必须有类/结构/联合 类型是'const char *const'
【问题讨论】:
-
该错误是否与您向我们展示的代码行之一有关?
-
您链接到的解决方案不使用
c_str()。我假设 boost 要求参数是以 STL 为中心的,即有一个begin()迭代器。 -
@DrewDormann 该错误是指 tokenizer.hpp: template
tokenizer(const Container& c,const TokenizerFunc& f) : first_(c.begin()), last_(c.end() ), f_(f) { } -
作为@PaulMcKenzie,它需要std::string,而不是const char*
标签: c++ c++11 boost tokenize boost-tokenizer