最終更新日時(UTC):
が更新

履歴 編集

function
<string>

std::to_wstring(C++11)

namespace std {
  wstring to_wstring(int val);
  wstring to_wstring(unsigned int val);
  wstring to_wstring(long val);
  wstring to_wstring(unsigned long val);
  wstring to_wstring(long long val);
  wstring to_wstring(unsigned long long val);
  wstring to_wstring(float val);
  wstring to_wstring(double val);
  wstring to_wstring(long double val);
}

概要

数値valwstring型文字列に変換する。

戻り値

各数値型に対して、swprintf(buf, buffsize, fmt, val)によって生成された文字列のwstringオブジェクトを返す。使用されるバッファサイズは未規定

各型で使用されるフォーマットは以下のようになる:

フォーマット
int L"%d"
unsigned int L"%u"
long L"%ld"
unsigned long L"%lu"
long long L"%lld"
unsigned long long L"%llu"
float L"%f"
double L"%f"
long double L"%Lf"

#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;
}

出力

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;
}

バージョン

言語

  • C++11

処理系

  • Clang: 3.0
  • GCC: 4.5.4
  • ICC: ?
  • Visual C++: 2010, 2012, 2013, 2015, 2017
    • 2010は、不完全な実装。以下の型のみ多重定義されている。
      • long long
      • unsigned long long
      • long double

関連項目

名前 参照
to_string 数値をstringに変換する

参照