iostream in c++ code example

Example 1: iostream library in cpp

// iostream_cerr.cpp
// compile with: /EHsc
#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 2: stringstream in c++

- A stringstream associates a string object with a stream allowing 
you to read from the string as if it were a stream (like cin).
- Method:
 clear() — to clear the stream
 str() — to get and set string object whose content is present in stream.
 operator << — add a string to the stringstream object.
 operator >> — read something from the stringstream object,

Example 3: stringstream in c++

std::stringstream os;
os << "12345 67.89"; // insert a string of numbers into the stream

std::string strValue;
os >> strValue;

std::string strValue2;
os >> strValue2;

// print the numbers separated by a dash
std::cout << strValue << " - " << strValue2 << std::endl;

Example 4: why we use iostream in C++ programming

#include <iostream>

Example 5: sstream c++

sstream  str()

Tags:

Cpp Example