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
  • 250,370
  • 26
  • 305
  • 497

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
  • 219,040
  • 71
  • 423
  • 503
6

Sure:

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

These functions use sprintf (or equivalent) internally.

Kerrek SB
  • 447,451
  • 88
  • 851
  • 1,056
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
  • 15,795
  • 16
  • 68
  • 133