c++ string to stream

C++
// EXAMPLE
ostringstream ssTextAsStream("This is part of the stream."); // declare ostringstream
string sTextAsString = ssTextAsStream.str(); // converted to string
cout << sTextAsString << "\n"; // printed out

/* SYNTAX
<YourStringStream>.str()
*/

/* HEADERS
#include <iostream>
#include <sstream>
using namespace std;
*/// CPP program to count words in a string 
// using stringstream. 
#include <bits/stdc++.h> 
using namespace std; 
  
int countWords(string str) 
{ 
    // breaking input into word using string stream 
    stringstream s(str); // Used for breaking words 
    string word; // to store individual words 
  
    int count = 0; 
    while (s >> word) 
        count++; 
    return count; 
} 
  
// Driver code 
int main() 
{ 
    string s = "geeks for geeks geeks "
               "contribution placements"; 
    cout << " Number of words are: " << countWords(s); 
    return 0; 
} 
// stringstream::str
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream, std::stringbuf

int main () {
  std::stringstream ss;
  ss.str ("Example string");
  std::string s = ss.str();
  std::cout << s << '\n';
  return 0;
}
Source

Also in C++: