我想遍历逗号分隔的字符串列表并对每个字符串进行处理。有没有办法设置 boost::split 以将“abc,xyz”和“abc”都识别为有效输入?换句话说,如果谓词不匹配任何内容,Split 是否可以返回整个输入字符串?
或者我应该改用 boost:tokenizer 吗?
我想遍历逗号分隔的字符串列表并对每个字符串进行处理。有没有办法设置 boost::split 以将“abc,xyz”和“abc”都识别为有效输入?换句话说,如果谓词不匹配任何内容,Split 是否可以返回整个输入字符串?
或者我应该改用 boost:tokenizer 吗?
Boost 1.54 完全符合您的要求。没试过新版本。
例子:
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
int main()
{
std::string test1 = "abc,xyz";
std::vector<std::string> result1;
boost::algorithm::split(result1, test1, boost::is_any_of(","));
for (auto const & s : result1)
{
std::cout << s << std::endl;
}
std::string test2 = "abc";
std::vector<std::string> result2;
boost::algorithm::split(result2, test2, boost::is_any_of(","));
for (auto const & s : result2)
{
std::cout << s << std::endl;
}
return 0;
}
产生:
abc
xyz
abc