-6

I have never used stringstream before and was given a sample code but with no explanation of what was happening in the code. If someone could explain each line's purpose that would be great. I have looked in multiple places but cant seem to pin down the second line.

#include <sstream> // i know this line includes the file

stringstream    ss(aStringVariable);// this line in particular 

ss >> aVariable;

getline(ss, stringVariable2HoldValue, ‘|’);
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Spencer
  • 1
  • 2

1 Answers1

1

There's a constructor for std::stringstream that takes a std::string as a parameter and initializes the stream with that value.

#include <iostream>
#include <sstream>
#include <string>

int main() {

    std::stringstream ss("foo bar");

    std::string str1, str2;
    ss >> str1 >> str2;

    std::cout << "str1: " << str1 << std::endl;
    std::cout << "str2: " << str2 << std::endl;

}

This code initializes a stringstream, ss, with the value "foo bar" and then reads it into two strings, str1 and str2, in the same way in which you would read from a file or std::cin.

Sam Marinelli
  • 999
  • 1
  • 6
  • 16