1// stringstream::str
2#include <string> // std::string
3#include <iostream> // std::cout
4#include <sstream> // std::stringstream, std::stringbuf
5
6int main () {
7 std::stringstream ss;
8 ss.str ("Example string");
9 std::string s = ss.str();
10 std::cout << s << '\n';
11 return 0;
12}
1#include <iostream>
2#include <sstream>
3
4std::string input = "abc,def,ghi";
5std::istringstream ss(input);
6std::string token;
7
8while(std::getline(ss, token, ',')) {
9 std::cout << token << '\n';
10}
1- A stringstream associates a string object with a stream allowing
2you to read from the string as if it were a stream (like cin).
3- Method:
4 clear() — to clear the stream
5 str() — to get and set string object whose content is present in stream.
6 operator << — add a string to the stringstream object.
7 operator >> — read something from the stringstream object,
8
1// EXAMPLE
2ostringstream ssTextAsStream("This is part of the stream."); // declare ostringstream
3string sTextAsString = ssTextAsStream.str(); // converted to string
4cout << sTextAsString << "\n"; // printed out
5
6/* SYNTAX
7<YourStringStream>.str()
8*/
9
10/* HEADERS
11#include <iostream>
12#include <sstream>
13using namespace std;
14*/
1std::stringstream os;
2os << "12345 67.89"; // insert a string of numbers into the stream
3
4std::string strValue;
5os >> strValue;
6
7std::string strValue2;
8os >> strValue2;
9
10// print the numbers separated by a dash
11std::cout << strValue << " - " << strValue2 << std::endl;
12