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)
Related Contents:
- How can I convert String to Int?
- How to convert a number to string and vice versa in C++
- How to convert string to char array in C++?
- How to convert string to integer in C#
- How can I create a string from a single character?
- c++ parse int from string [duplicate]
- How to convert int to string in C++
- How do I iterate over the words of a string?
- std::string formatting like sprintf
- What happens if I assign a negative value to an unsigned variable?
- Is there a simple way to convert C++ enum to string?
- C++ convert string to hexadecimal and vice versa
- Why is strncpy insecure?
- How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC?
- How do I convert a double into a string in C++?
- “\n” or ‘\n’ or std::endl to std::cout? [duplicate]
- How to convert integer to string in C? [duplicate]
- Efficient string concatenation in C++
- Conversion constructor vs. conversion operator: precedence
- Using getline() in C++
- What’s the difference between size_t and int in C++?
- Why does streaming a char pointer to cout not print an address?
- Fast String Hashing Algorithm with low collision rates with 32 bit integer [closed]
- How to convert QString to std::string?
- C/C++: Optimization of pointers to string constants
- Count character occurrences in a string in C++
- Why can’t I write to a string literal while I *can* write to a string object?
- How to use string.substr() function?
- C++ convert vector to vector
- Set precision of std::to_string when converting floating point values [duplicate]
- Convert wstring to string encoded in UTF-8
- How to store extremely large numbers?
- Why can’t the switch statement be applied to strings?
- Deprecated conversion from string literal to ‘char*’
- Conversion from string literal to char* is deprecated
- invalid conversion from ‘const char*’ to ‘char’
- Difference between and ?
- Converting bool to text in C++
- Most optimized way of concatenation in strings
- Why is modifying a string through a retrieved pointer to its data not allowed?
- C++ string to double conversion
- Why does libc++’s implementation of std::string take up 3x memory as libstdc++?
- What is the proper function for comparing two C-style strings?
- Modifying a char *const string
- Why am I getting string does not name a type Error?
- initializing std::string from char* without copy
- Copy a streambuf’s contents to a string
- C++ floating point to integer type conversions
- Can I get a non-const C string back from a C++ string?
- Is writing to &str[0] buffer (of a std:string) well-defined behaviour in C++11?