- string[meta header]
- std[meta namespace]
- function[meta id-type]
- cpp11[meta cpp]
namespace std {
wstring to_wstring(int val); // (1) C++11
constexpr wstring to_wstring(int val); // (1) C++26
wstring to_wstring(unsigned int val); // (2) C++11
constexpr wstring to_wstring(unsigned int val); // (2) C++26
wstring to_wstring(long val); // (3) C++11
constexpr wstring to_wstring(long val); // (3) C++26
wstring to_wstring(unsigned long val); // (4) C++11
constexpr wstring to_wstring(unsigned long val); // (4) C++26
wstring to_wstring(long long val); // (5) C++11
constexpr wstring to_wstring(long long val); // (5) C++26
wstring to_wstring(unsigned long long val); // (6) C++11
constexpr wstring to_wstring(unsigned long long val); // (6) C++26
wstring to_wstring(float val); // (7) C++11
wstring to_wstring(double val); // (8) C++11
wstring to_wstring(long double val); // (9) C++11
}- wstring[link basic_string.md]
数値valをwstring型文字列に変換する。
- (1) :
int型の値をwstring型に変換する - (2) :
unsigned int型の値をwstring型に変換する - (3) :
long型の値をwstring型に変換する - (4) :
unsigned long型の値をwstring型に変換する - (5) :
long long型の値をwstring型に変換する - (6) :
unsigned long long型の値をwstring型に変換する - (7) :
float型の値をwstring型に変換する - (8) :
double型の値をwstring型に変換する - (9) :
long double型の値をwstring型に変換する
-
C++11まで 各数値型に対して、
swprintf(buf, buffsize, fmt, val)によって生成された文字列のwstringオブジェクトを返す。使用されるバッファサイズは未規定。各型で使用されるフォーマットは以下のようになる:
型 フォーマット intL"%d"unsigned intL"%u"longL"%ld"unsigned longL"%lu"long longL"%lld"unsigned long longL"%llu"floatL"%f"doubleL"%f"long doubleL"%Lf" -
C++26から
return format(L"{}", val);
- format[link /reference/format/format.md]
#include <iostream>
#include <string>
int main()
{
std::wstring s1 = std::to_wstring(123);
std::wcout << s1 << std::endl;
std::wstring s2 = std::to_wstring(3.14);
std::wcout << s2 << std::endl;
}- std::to_wstring[color ff0000]
123
3.140000
#include <cstdio>
#include <string>
#include <limits>
std::wstring to_wstring(int val)
{
const std::size_t size = std::numeric_limits<int>::digits10 + 1
+ 2; // '-' + '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%d", val);
return buffer;
}
std::wstring to_wstring(unsigned int val)
{
const std::size_t size = std::numeric_limits<unsigned int>::digits10 + 1
+ 1; // '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%u", val);
return buffer;
}
std::wstring to_wstring(long val)
{
const std::size_t size = std::numeric_limits<long>::digits10 + 1
+ 2; // '-' + '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%ld", val);
return buffer;
}
std::wstring to_wstring(unsigned long val)
{
const std::size_t size = std::numeric_limits<unsigned long>::digits10 + 1
+ 1; // '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%lu", val);
return buffer;
}
std::wstring to_wstring(long long int val)
{
const std::size_t size = std::numeric_limits<long long int>::digits10 + 1
+ 2; // '-' + '\0';
wchar_t buffer[size];
std::swprintf(buffer, size, L"%lld", val);
return buffer;
}
std::wstring to_wstring(unsigned long long int val)
{
const std::size_t size = std::numeric_limits<unsigned long long int>::digits10 + 1
+ 1; // '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%llu", val);
return buffer;
}
std::wstring to_wstring(float val)
{
const std::size_t size = std::numeric_limits<float>::max_exponent10 + 1
+ 6 // fixed precision (printf's default)
+ 3; // '-' + '.' + '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%f", val);
return buffer;
}
std::wstring to_wstring(double val)
{
const std::size_t size = std::numeric_limits<double>::max_exponent10 + 1
+ 6 // fixed precision (printf's default)
+ 3; // '-' + '.' + '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%f", val);
return buffer;
}
std::wstring to_wstring(long double val)
{
const std::size_t size = std::numeric_limits<long double>::max_exponent10 + 1
+ 6 // fixed precision (printf's default)
+ 3; // '-' + '.' + '\0'
wchar_t buffer[size];
std::swprintf(buffer, size, L"%Lf", val);
return buffer;
}- digits10[link /reference/limits/numeric_limits/digits10.md]
- max_exponent10[link /reference/limits/numeric_limits/max_exponent10.md]
- C++11
- Clang: 3.0 [mark verified]
- GCC: 4.5.4 [mark verified]
- ICC: ?
- Visual C++: 2010 [mark verified], 2012 [mark verified], 2013 [mark verified], 2015 [mark verified], 2017 [mark verified]
- 2010は、不完全な実装。以下の型のみ多重定義されている。
long longunsigned long longlong double
- 2010は、不完全な実装。以下の型のみ多重定義されている。
| 名前 | 参照 |
|---|---|
to_string |
数値をstringに変換する |
- N2408 Simple Numeric Access Revision 2
- P2587R3
to_stringor notto_string- C++26から
sprintfベースの仕様をやめてstd::format()ベースの仕様になった
- C++26から
- P3391R2
constexpr std::format- C++26から整数バージョンが
constexprに対応した
- C++26から整数バージョンが