Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to convert int to string in C++

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

(1)

int a = 10; char *intStr = itoa(a); string str = string(intStr); 

(2)

int a = 10; stringstream ss; ss << a; string str = ss.str(); 
like image 814
Nemo Avatar asked Apr 08 '11 04:04

Nemo


People also ask

Can you convert int to char in C?

We can convert an integer to the character by adding a '0' (zero) character. The char data type is represented as ascii values in c programming. Ascii values are integer values if we add the '0' then we get the ASCII of that integer digit that can be assigned to a char variable.

Can we use stoi in C?

What Is stoi() in C++? In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings. The stoi() function is relatively new, as it was only added to the language as of its latest revision (C++11) in 2011.

How does ITOA work in C?

C Programming/stdlib. h/itoaitoa takes the integer input value input and converts it to a number in base radix . The resulting number (a sequence of base- radix digits) is written to the output buffer buffer .


2 Answers

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string.

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

is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword:

auto s = std::to_string(42); 

Note: see [string.conversions] (21.5 in n3242)

like image 140
Matthieu M. Avatar answered Sep 22 '22 05:09

Matthieu M.


C++20 update: std::format would be the idiomatic way now.


C++17 update:

Picking up a discussion with @v.oddou a couple of years later, C++17 has finally delivered a way to do the originally macro-based type-agnostic solution (preserved below) without going through macro uglyness.

// variadic template template < typename... Args > std::string sstr( Args &&... args ) {     std::ostringstream sstr;     // fold expression     ( sstr << std::dec << ... << args );     return sstr.str(); } 

Usage:

int i = 42; std::string s = sstr( "i is: ", i ); puts( sstr( i ).c_str() );  Foo x( 42 ); throw std::runtime_error( sstr( "Foo is '", x, "', i is ", i ) ); 

Original (C++98) answer:

Since "converting ... to string" is a recurring problem, I always define the SSTR() macro in a central header of my C++ sources:

#include <sstream>  #define SSTR( x ) static_cast< std::ostringstream & >( \         ( std::ostringstream() << std::dec << x ) ).str() 

Usage is as easy as could be:

int i = 42; std::string s = SSTR( "i is: " << i ); puts( SSTR( i ).c_str() );  Foo x( 42 ); throw std::runtime_error( SSTR( "Foo is '" << x << "', i is " << i ) ); 

The above is C++98 compatible (if you cannot use C++11 std::to_string), and does not need any third-party includes (if you cannot use Boost lexical_cast<>); both these other solutions have a better performance though.

like image 40
DevSolar Avatar answered Sep 21 '22 05:09

DevSolar