To render std::string
content as is use a raw string literal. The following code would retrieve all single lines from a raw string literal:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
int main() {
std::string s = R"x(Hello
Welcome
Oranges
Bananas
Hi
Triangle (and hey, some extra stuff in the line with parenthesis)
)x";
std::istringstream iss(s);
std::vector<std::string> lines;
std::string line;
while(getline(iss,line)) {
lines.push_back(line);
}
for(const auto& line : lines) {
std::cout << line << '\n';
}
}
See the working version online here.
With prior c++11 standards, you'll have to escape the line breaks with a \
character like so:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
int main() {
std::string s = "Hello \n\
Welcome \n\
Oranges \n\
Bananas \n\
Hi \n\
Triangle (and hey, some extra stuff in the line with parenthesis)";
std::istringstream iss(s);
std::vector<std::string> lines;
std::string line;
while(getline(iss,line)) {
lines.push_back(line);
}
for(std::vector<std::string>::const_iterator it = lines.begin(); it != lines.end(); ++it) {
std::cout << *it << '\n';
}
}
See the other working online example.