str() c++ code example

Example 1: convert string to stream c++

// 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;
}

Example 2: string in cpp

// Include the string library
#include <string>

// Create a string variable
string greeting = "Hello";

Example 3: c++ string

#include <string>

std::string myString = "Hello, World!";

Tags:

Cpp Example