Есть ли способ использовать boost :: split для разделения строки при обнаружении пустой строки?

Вот отрывок из того, что я имею в виду.

std::stringstream source;
source.str(input_string);
std::string line;
std::getline(source, line, '\0');
std::vector<std::string> token;
boost:split(token,line, boost::is_any_of("what goes here for blank line");
2
zstreet 20 Июн 2020 в 23:32

1 ответ

Лучший ответ

Вы можете разделить на двойной \n\n, если вы не имели в виду пустую строку как «строку, которая может содержать другие пробелы».

Live On Coliru

#include <boost/regex.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <sstream>
#include <iostream>
#include <iomanip>

int main() {
    std::stringstream source;
    source.str(R"(line one

that was an empty line, now some whitespace:
      
bye)");

    std::string line(std::istreambuf_iterator<char>(source), {});
    std::vector<std::string> tokens;

    auto re = boost::regex("\n\n");
    boost::split_regex(tokens, line, re);

    for (auto token : tokens) {
        std::cout << std::quoted(token) << "\n";
    }
}

Печать

"line one"
"that was an empty line, now some whitespace:
      
bye"

Разрешить пробелы в «пустых» строках

Просто выразите это регулярным выражением:

auto re = boost::regex(R"(\n\s*\n)");

Теперь вывод: Live On Coliru .

"line one"
"that was an empty line, now some whitespace:"
"bye"
2
sehe 20 Июн 2020 в 21:17