sstream c++ code example
Example 1: convert string to stream c++
#include <string>
#include <iostream>
#include <sstream>
int main () {
std::stringstream ss;
ss.str ("Example string");
std::string s = ss.str();
std::cout << s << '\n';
return 0;
}
Example 2: iostream library in cpp
#include <iostream>
#include <fstream>
using namespace std;
void TestWide( )
{
int i = 0;
wcout << L"Enter a number: ";
wcin >> i;
wcerr << L"test for wcerr" << endl;
wclog << L"test for wclog" << endl;
}
int main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
TestWide( );
}
Example 3: c++ string to stream
ostringstream ssTextAsStream("This is part of the stream.");
string sTextAsString = ssTextAsStream.str();
cout << sTextAsString << "\n";
Example 4: stringstream in c++
std::stringstream os;
os << "12345 67.89";
std::string strValue;
os >> strValue;
std::string strValue2;
os >> strValue2;
std::cout << strValue << " - " << strValue2 << std::endl;
Example 5: sstream c++
sstream str()