C++ String Streams Initialization

Introduction

To initialize a string stream with a string we use:

#include <iostream> 
#include <sstream> 

int main() /* ww w .  j  a va2s . c o m*/
{ 
    std::stringstream ss; 
    ss << "Hello World."; 
    std::cout << ss.str(); 
} 

We use the string stream's member function .str() to assign the string stream's content to a string variable:

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

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



PreviousNext

Related