C++ String Streams Insert

Introduction

To insert data into a string stream, we use the formatted output operator << :

#include <iostream> 
#include <string> 
#include <sstream> 

int main() //from   w  w w. j a va  2s .c  o  m
{ 
    std::string s = "Hello World."; 
    std::stringstream ss{ s }; 
    std::cout << ss.str(); 
} 

We can insert values of fundamental types into a string stream using the formatted output operator <<:

#include <iostream> 
#include <sstream> 

int main() //  w  ww .ja  va2  s .c om
{ 
    char c = 'A'; 
    int x = 123; 
    double d = 456.78; 
    std::stringstream ss; 
    ss << c << x << d; 
    std::cout << ss.str(); 
} 



PreviousNext

Related