C++ String to_string()
The std::to_string()
function in C++ converts numerical values into their string representation. Introduced in C++11 and defined in the <string>
header, this function is used to transform various numeric types into std::string
objects.
Syntax of string to_string() function
</>
Copy
std::string to_string(int val);
std::string to_string(long val);
std::string to_string(long long val);
std::string to_string(unsigned val);
std::string to_string(unsigned long val);
std::string to_string(unsigned long long val);
std::string to_string(float val);
std::string to_string(double val);
std::string to_string(long double val);
Parameters of string to_string() function
Parameter | Description |
---|---|
val | The numerical value to be converted to a string. It can be of any numeric data type such as int , long , float , double , etc. |
Return Value of string to_string() function
The function returns a std::string
object containing the textual representation of the input numerical value.
Exceptions for string to_string() function
The std::to_string()
function may throw a std::bad_alloc
exception if there is insufficient memory to allocate the resulting string.
Examples for string to_string() function
Example 1: Converting an Integer to String
In this example, we convert an integer value to a string using std::to_string()
and concatenate it with another string.
Program
</>
Copy
#include <iostream>
#include <string>
int main() {
int number = 42;
std::string result = "The answer is " + std::to_string(number);
std::cout << result << std::endl;
return 0;
}
Output
The answer is 42
Explanation
- The program includes the necessary headers:
<iostream>
for input and output operations, and<string>
for string handling. - An integer variable
number
is initialized with the value42
. - The
std::to_string()
function converts the integer to a string, which is then concatenated with another string to form the final message. - The program outputs the concatenated string using
std::cout
.
Example 2: Converting a Floating-Point Number to String
This example demonstrates converting a floating-point number to a string and appending additional text.
Program
</>
Copy
#include <iostream>
#include <string>
int main() {
double pi = 3.141592653589793;
std::string result = "The value of pi is approximately " + std::to_string(pi);
std::cout << result << std::endl;
return 0;
}
Output
The value of pi is approximately 3.141593
Explanation
- The program includes the necessary headers:
<iostream>
for input/output operations and<string>
for string handling. - A double variable
pi
is initialized with the value of π (pi). - The
std::to_string()
function converts the double to a string. Note that the function may limit the precision of floating-point numbers, typically to six decimal places. - The resulting string is concatenated with additional text and printed using
std::cout
.
Notes
- When converting floating-point numbers,
std::to_string()
may not preserve the full precision of the value. For higher precision requirements, consider usingstd::ostringstream
with appropriate formatting settings. - The function relies on the current C locale for formatting purposes, which may affect the output in multi-threaded environments.