Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 to_string() function, where? [closed]

Tags:

c++

c++11

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?

like image 316
catfish_deluxe_call_me_cd Avatar asked Sep 22 '11 21:09

catfish_deluxe_call_me_cd


2 Answers

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.

like image 65
R. Martinho Fernandes Avatar answered Oct 12 '22 12:10

R. Martinho Fernandes


Sure:

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

These functions use sprintf (or equivalent) internally.

like image 37
Kerrek SB Avatar answered Oct 12 '22 12:10

Kerrek SB