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
.
solved Understanding stringstream [closed]