C++ String to_wstring
The std::to_wstring()
function in C++ converts numerical values into their wide string representation. Introduced in C++11 and defined in the <string>
header, this function is used to transform of various numeric types into std::wstring
objects.
Syntax of string to_wstring function
</>
Copy
std::wstring to_wstring(int val);
std::wstring to_wstring(long val);
std::wstring to_wstring(long long val);
std::wstring to_wstring(unsigned val);
std::wstring to_wstring(unsigned long val);
std::wstring to_wstring(unsigned long long val);
std::wstring to_wstring(float val);
std::wstring to_wstring(double val);
std::wstring to_wstring(long double val);
Parameters of string to_wstring function
Parameter | Description |
---|---|
val | The numerical value to be converted to a wide string. It can be of any numeric data type such as int , long , float , double , etc. |
Return Value of string to_wstring function
The function returns a std::wstring
object containing the textual representation of the input numerical value.
Exceptions for string to_wstring function
The std::to_wstring()
function may throw a std::bad_alloc
exception if there is insufficient memory to allocate the resulting wide string.
Examples for string to_wstring function
Example 1: Converting an Integer to Wide String
In this example, we convert an integer value to a wide string using std::to_wstring()
and concatenate it with another wide string.
Program
</>
Copy
#include <iostream>
#include <string>
int main() {
int number = 42;
std::wstring result = L"The answer is " + std::to_wstring(number);
std::wcout << 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_wstring()
function converts the integer to a wide string, which is then concatenated with another wide string to form the final message. - The program outputs the concatenated wide string using
std::wcout
.
Example 2: Converting a Floating-Point Number to Wide String
This example demonstrates converting a floating-point number to a wide string and appending additional text.
Program
</>
Copy
#include <iostream>
#include <string>
int main() {
double pi = 3.141592653589793;
std::wstring result = L"The value of pi is approximately " + std::to_wstring(pi);
std::wcout << 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_wstring()
function converts the double to a wide string. Note that the function may limit the precision of floating-point numbers, typically to six decimal places. - The resulting wide string is concatenated with additional text and printed using
std::wcout
.
Notes
- When converting floating-point numbers,
std::to_wstring()
may not preserve the full precision of the value. For higher precision requirements, consider usingstd::wostringstream
with appropriate formatting settings. - The function relies on the current C locale for formatting purposes, which may affect the output in multi-threaded environments.
- Ensure that your environment supports wide character output when using
std::wcout
, as some systems may require specific configurations to handle wide strings properly.