【问题标题】:boost::split(result, input, boost::is_any_of("(, )")) can't split empty spaceboost::split(result, input, boost::is_any_of("(, )")) 不能分割空白空间
【发布时间】:2020-06-19 09:57:57
【问题描述】:

代码是:

vector<string> result;
string input = "Ellipse(50, 50, 200, 300)"
boost::split(result, input, boost::is_any_of("(, )"))

int i=0;
for (auto at=result.begin(); at != result.end(); at++)
    cout << ++i << " " << *at << endl;

输出是:

1 Ellipse
2 50
3 
4 50
5 
6 200
7
8 300
9

我只想得到椭圆和整数,而不是空格。

我怎样才能得到正确的结果?

输入字符串不能更改。

【问题讨论】:

  • 在字符串中尝试过反斜杠空格吗? "(,\ )"

标签: c++ boost split


【解决方案1】:

这将正确删除空格:erase_all(str1, " ");

另一种选择是使用boost::token_compress_on

boost::trim_if(input, boost::is_any_of(" "));
boost::split(result, input, boost::is_any_of("(, )"), boost::token_compress_on);

这个问题已经被问过了:boost::split leaves empty tokens at the beginning and end of string - is this desired behaviour?How to use boost split to split a string and ignore empty values?

【讨论】:

  • 问题是他有两个连续的token,','' ':他得到的空元素是它们之间的空字符串。使用boost::token_compress_on其实是解决他的问题,其他只是变通方法。
猜你喜欢
  • 2011-12-17
  • 2011-07-04
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多