13

See the N3242 Working Draft of C++11, chapter 21.5 Numeric Conversions.

There are some useful functions, such as string to_string(int val); mentioned but I don't understand how they're called. Can anyone give me an example please?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524

3 Answers3

28

Those functions are in the header <string>. You just call them like any other function:

#include <string>
std::string answer = std::to_string(42);

GCC 4.5 already supports those functions, you just need to compile with the -std=c++0x flag.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
6

Sure:

std::string s = std::to_string(123);  // now s == "123"

These functions use sprintf (or equivalent) internally.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

They are called like any other function:

int number = 10;
std::string value;
value = std::to_string(number);
std::cout << value;

To call them you will need a C++ compiler that supports the draft recommendations (VS2010 and GCC4+ I think support them).

graham.reeds
  • 16,230
  • 17
  • 74
  • 137