c ++通过双换行拆分字符串

non*_*one 7 c++ string split

我一直试图用双换行符分割一个字符串("\n\n").

input_string = "firstline\nsecondline\n\nthirdline\nfourthline";

size_t current;
size_t next = std::string::npos;
do {
  current = next + 1;
  next = input_string.find_first_of("\n\n", current);
  cout << "[" << input_string.substr(current, next - current) << "]" << endl;
} while (next != std::string::npos);
Run Code Online (Sandbox Code Playgroud)

给了我输出

[firstline]
[secondline]
[]
[thirdline]
[fourthline]
Run Code Online (Sandbox Code Playgroud)

这显然不是我想要的.我需要得到类似的东西

[first line
second line]
[third line
fourthline]
Run Code Online (Sandbox Code Playgroud)

我也试过,boost::split但它给了我相同的结果.我错过了什么?

Ben*_*ley 5

find_first_of只查找单个字符.通过传递它告诉它要做的"\n\n"是找到第一个'\n'或者'\n',这是多余的.请string::find改用.

boost::split 也可以通过一次只检查一个字符来工作.