Use std::to_string for most conversions
To convert an int to a std::string in C++, call std::to_string(). It takes a number and returns a new std::string that holds its digits. It was added in C++11, and you need the <string> header for it.
#include <iostream>
#include <string>
int main() {
int score = 95;
std::string text = std::to_string(score);
std::cout << "Score: " + text << '\n'; // Score: 95
std::cout << text.length() << '\n'; // 2
}
The original score does not change. Negative numbers keep their minus sign, so std::to_string(-17) returns "-17". Joining the result with other text, as the example does, is covered in how to concatenate strings in C++. For the full set of types the function accepts, see what does to_string() do in C++.
Use a string stream
A std::ostringstream accepts values with <<, the same way std::cout does. The difference is that the output goes into a string, which is handy when you are building a longer line of text.
#include <iostream>
#include <sstream>
#include <string>
int main() {
int score = 95;
std::ostringstream out;
out << "Score: " << score;
std::string text = out.str();
std::cout << text << '\n'; // Score: 95
}
String streams have been in the standard library since C++98, so this approach works even when you cannot rely on C++11. The wider topic is covered in stringstream in C++.
Use std::format in C++20
With C++20, std::format() builds a string from a pattern. Each {} is replaced by the next argument, which keeps the text and the value in one place.
#include <format>
#include <iostream>
#include <string>
int main() {
int score = 95;
std::string text = std::format("Score: {}", score);
std::cout << text << '\n'; // Score: 95
}
With GCC or Clang, compile with -std=c++20 or later. Older compilers and standard libraries may not provide <format>, so use std::to_string or a string stream there.
Write into a buffer with std::to_chars
std::to_chars() (C++17, in <charconv>) writes the digits into a character array that you provide. The function itself does not allocate memory. The array must be large enough, and twelve characters are enough for a 32-bit int, including a minus sign.
#include <charconv>
#include <iostream>
#include <string>
int main() {
int score = 95;
char buffer[12];
std::to_chars_result result = std::to_chars(buffer, buffer + sizeof(buffer), score);
std::string text(buffer, result.ptr);
std::cout << text << '\n'; // 95
}
The function does not add a null terminator, so the code builds the string from the start of the buffer up to the pointer it returns. The result also has an error field, ec, which you can check when the buffer might be too small. For how large each integer type can be, see primitive data types in C++.
Common mistakes
- Passing a char.
std::to_string('7')returns"55", not"7". Acharis promoted to anint, so you get its character code. To get the digit value, subtract'0'first:std::to_string('7' - '0')returns"7". The single-character case is covered in how to convert a char to an int in C++. - Passing a double.
std::to_string(3.14159)returns"3.141590". It always prints six digits after the decimal point, so it gives you little control over the format. A string stream orstd::formatis better when the layout matters. - Expecting the constructor to convert.
std::string(42)does not compile, andstd::string(3, '7')creates"777", three copies of the character. Neither gives you the text"42"or"3". The question of whetherint()can read a string is covered in can you use int() on a string.
Which method should you use?
- Use
std::to_string()for most code that targets C++11 or later. - Use a string stream when you are combining several values into one string.
- Use
std::format()in C++20 when you want a pattern with placeholders. - Use
std::to_chars()when you want to write into your own buffer.
Going back from a string to an int
The reverse direction has its own guide: how to convert a string to an int in C++. The short version is that you use std::stoi() to parse the text, rather than trying to cast it with int().