String stream in C++

Level 3109 keystrokes

A stringstream works like cout but writes into a string. Text and numbers are combined with <<, and str() returns the result "Value: 42, Pi: 3.14".

C++ code

// An output target in memory (needs <sstream>)
1stringstream ss;
// << converts the numbers to text and appends everything, just like with cout
2ss << "Value: " << 42 << ", Pi: " << 3.14;
// str() returns the accumulated text as a string
3string result = ss.str();
// Prints Value: 42, Pi: 3.14
4cout << result << endl;

Output

Value: 42, Pi: 3.14
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 12
  • ;× 4
  • "× 4
  • :× 2
  • .× 2
  • ,× 1
  • (× 1
  • )× 1
  • =× 1

More C++ examples at level 3

← C++ typing practice