previous | start | next

## String Streams

- By writing to a string stream, you can convert numbers to strings.

  >     ostringstream outstr;

- By using the << operator, the number is converted into a sequence of characters.

  >     outstr << setprecision(5) << sqrt(2);

- To obtain a string from the stream, call the `str` member function.

  >     string output = outstr.str();

- Example: (builds the string "January 23, 1955")

  >     string month = "January";
  >     int day = 23;
  >     int year = 1955;
  >     ostringstream outstr;
  >     outstr << month << " " << day << "," << year;
  >     string output = outstr.str();

- Converting an integer into a string is such a common operation that is useful to have a helper function for it.

  >     string int_to_string(int n)
  >     {
  >        ostringstream outstr;
  >        outstr << n;
  >        return outstr.str();
  >     }

previous | start | next