boost::split 可以用在只有一个令牌的列表上吗?

Can boost::split Be Used on a List of Only One Token?

我想遍历以逗号分隔的字符串列表,并对每个字符串执行操作。有没有办法设置 boost::split 以将 "abc,xyz" 和 "abc" 都识别为有效输入?换句话说,如果谓词不匹配任何内容,是否可以拆分 return 整个输入字符串?

或者我应该改用 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