String Conversions: to_string and stoi
Convert between strings and numeric types safely with std::to_string, std::stoi and std::stod.
Why String Conversions Matter
You constantly need to convert between numbers and text — for user input, configuration files, JSON, URLs, and logs.
std::to_string
std::to_string converts any built-in numeric type to a string. It uses default formatting.
#include <string>
std::string a = std::to_string(42); // "42"
std::string b = std::to_string(-7); // "-7"
std::string c = std::to_string(3.14); // "3.140000"All lessons in this course
- std::string vs C-Style Char Arrays
- String Operations: concat substr find replace
- String Stream stringstream for Parsing
- String Conversions: to_string and stoi