-1

One of my assignments involves reading several strings into my program. I've figured out how to store the strings into one, but now I need to retrieve each string by line. Any suggestions? Example:

string s = "Hello
            Welcome
            Oranges
            Bananas
            Hi
            Triangle"

I'm not allowed to store them into an array either; they must all be contained in one string.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
Bagman
  • 11

2 Answers2

1

I would probably put the string into stringstream, then use something like std::getline to read a line at a time from the string stream. If you're really worried about execution speed, there are faster ways, but that would be the obvious first choice until profiling told you it wasn't allowable.

For what it's worth, to embed a new-line into the string, you can use either \n, or switch to using a raw string literal:

string s = R"(Hello
              Welcome
              Oranges
              Bananas
              Hi
              Triangle)";

With a raw string literal, embedded new-lines (and anything else) becomes part of the string itself. In a normal string literal, you have to use \n instead (if you want it to be portable, anyway).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Good attempt, but my answer covers the parenthesis as well now ;-) – πάντα ῥεῖ Oct 08 '18 at 20:21
  • 1
    @πάνταῥεῖ: Covers a case that's not requested by the OP, but adds another case (not requested by the OP either) for which it fails. Doesn't seem like a huge improvement to me, but perhaps it's useful. As a rule, I don't favor posting a complete solution to obvious homework though. – Jerry Coffin Oct 08 '18 at 20:56
0

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190