I have a text in a std::string object. The text consists of several lines. I want to iterate over the text line by line using STL (or Boost). All solutions I come up with seem to be far from elegant. My best approach is to split the text at the line breaks. Is there a more elegant solution?
UPDATE: This is what I was looking for:
std::string input;
// get input ...
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
std::cout << line << std::endl;
}
Why do you keep the text in your source file? Keep it in a separate text file. Open it with std::ifstream and iterate over it with
while(getline(...))Alternatively, if the text HAS to be in a
std::stringvariable read line by line usingstd::istringstreamin a similar mannerIf your question is how to put the text lexially into your code without using +, please note that adjacent string literals are concatenated before compilation, so you could do this: