Convert an int to a string (C++)

Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > How do I... (Level 1) > Convert an int to a string (C++)

This item was added on: 2003/02/19

The following code shows how to convert an int to a C++ string, using a stringstream object:

#include <iostream>
#include <sstream>  // Required for stringstreams
#include <string> 

std::string IntToString ( int number )
  std::ostringstream oss;

  // Works just like cout
  oss<< number;

  // Return the underlying string
  return oss.str();

int main()
  int number = 12345;
  std::string result = IntToString ( number );

  // Now we can use concatenation on the number!
  std::cout<<"~~{" + result + "}~~\n";

Credit: Prelude

Script provided by SmartCGIs