Formatting Data with stringstream - C++ STL

C++ examples for STL:string

Description

Formatting Data with stringstream

Demo Code

#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <ios> 


using namespace std; 

int main(int argc, char *argv [])
{   /*  w w  w . j av  a  2  s. c  o m*/

        stringstream myStringStream; 
  
        myStringStream << "Hi" << endl; 
  
        cout << "width: " << myStringStream.width() << endl; 
        myStringStream << 123 << endl; 
        myStringStream << setw(10) << 123 << endl; 
        myStringStream << setw(0) << 123 << endl; 
  
        myStringStream << 123.567 << endl; 
        myStringStream << setprecision(4) << 123.567 << endl; 
        myStringStream << fixed << setprecision(2) << 123.567 << endl; 
        myStringStream << defaultfloat << setprecision(0) << 123.567 << endl; 
  
        myStringStream << 110 << endl << showbase; 
        myStringStream << hex << 110 << endl; 
        myStringStream << oct << 110 << endl; 
        myStringStream << dec << 110 << endl << noshowbase; 
  
        string myString = myStringStream.str(); 
        cout << myString; 
        return 0; 
}

Result


Related Tutorials