std::wcstold をマスターしよう! ワイド文字列を数値に変換する C++ の便利関数


std::wcstold 関数は、ワイド文字列 (wchar_t 型) を数値に変換するための関数です。std::stod 関数と似ていますが、ワイド文字列を扱う点が異なります。

ヘッダーファイル

この関数を使用するには、以下のヘッダーファイルをインクルードする必要があります。

#include <cstdlib>

関数プロトタイプ

long double std::wcstold(
    const wchar_t* str,
    wchar_t** endptr
);

引数

  • endptr: 変換が終了した文字列の次のポインタ。このポインタは、std::wcstold 関数によって更新されます。
  • str: 数値を含むワイド文字列へのポインタ

戻り値

  • 失敗した場合: 0 を返し、errno 変数にエラーコードを設定
  • 成功した場合: 変換された数値 (long double 型)

エラー

以下のエラーが発生する可能性があります。

  • ERANGE: 変換結果が long double 型の範囲外
  • EINVAL: str が nullptr または不正な形式

使い方

以下の例は、std::wcstold 関数の基本的な使い方を示しています。

#include <cstdlib>
#include <iostream>

int main() {
  wchar_t str[] = L"12.345";
  wchar_t* endptr;
  long double value = std::wcstold(str, &endptr);

  if (errno == 0) {
    std::cout << "変換された数値: " << value << std::endl;
    std::cout << "変換が終了した文字列: " << endptr << std::endl;
  } else {
    std::cout << "エラーが発生しました: " << std::strerror(errno) << std::endl;
  }

  return 0;
}

この例では、std::wcstold 関数は "12.345" というワイド文字列を long double 型の 12.345 に変換します。

  • ワイド文字列を扱う場合は、std::wstring クラスを使用すると便利です。
  • std::wcstold 関数は、ロケールによって影響を受ける場合があります。詳細は、std::setlocale 関数を参照してください。
  • C++ チュートリアル
  • C++ 標準ライブラリ リファレンス


例 1: ワイド文字列を数値に変換

#include <cstdlib>
#include <iostream>

int main() {
  wchar_t str[] = L"12.345";
  wchar_t* endptr;
  long double value = std::wcstold(str, &endptr);

  if (errno == 0) {
    std::cout << "変換された数値: " << value << std::endl;
    std::cout << "変換が終了した文字列: " << endptr << std::endl;
  } else {
    std::cout << "エラーが発生しました: " << std::strerror(errno) << std::endl;
  }

  return 0;
}

例 2: 基数を指定してワイド文字列を数値に変換

#include <cstdlib>
#include <iostream>

int main() {
  wchar_t str[] = L"1011";
  wchar_t* endptr;
  int base = 2;
  long double value = std::wcstold(str, &endptr, base);

  if (errno == 0) {
    std::cout << "変換された数値: " << value << std::endl;
    std::cout << "変換が終了した文字列: " << endptr << std::endl;
  } else {
    std::cout << "エラーが発生しました: " << std::strerror(errno) << std::endl;
  }

  return 0;
}

この例では、std::wcstold 関数は "1011" というワイド文字列を 2 進数で解釈し、long double 型の 11 に変換します。

例 3: エラー処理

#include <cstdlib>
#include <iostream>

int main() {
  wchar_t str[] = L"abc";
  wchar_t* endptr;
  long double value = std::wcstold(str, &endptr);

  if (errno == 0) {
    std::cout << "変換された数値: " << value << std::endl;
    std::cout << "変換が終了した文字列: " << endptr << std::endl;
  } else {
    std::cout << "エラーが発生しました: " << std::strerror(errno) << std::endl;
  }

  return 0;
}

この例では、std::wcstold 関数は "abc" というワイド文字列を数値に変換できないため、エラーが発生します。

例 4: std::wstring クラスを使用

#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::wstring str = L"12.345";
  wchar_t* cstr = str.c_str();
  wchar_t* endptr;
  long double value = std::wcstold(cstr, &endptr);

  if (errno == 0) {
    std::cout << "変換された数値: " << value << std::endl;
    std::cout << "変換が終了した文字列: " << endptr << std::endl;
  } else {
    std::cout << "エラーが発生しました: " << std::strerror(errno) << std::endl;
  }

  return 0;
}

この例では、std::wstring クラスを使用してワイド文字列を作成し、std::wcstold 関数に渡しています。

#include <cstdlib>
#include <iostream>
#include <locale>

int main() {
  std::locale::setDefault(std::locale("ja_JP.UTF-8"));

  wchar_t str[] = L"12,345";
  wchar_t* endptr;
  long double value = std::wcstold(str, &endptr);

  if (errno == 0) {
    std::cout <<


std::stod 関数

std::stod 関数は、標準的な C++ ライブラリに含まれている関数で、std::wcstold 関数とほぼ同じ機能を提供します。唯一の違いは、std::stod 関数はバイト文字列 (char 型) を扱う点です。

#include <cstdlib>
#include <iostream>

int main() {
  char str[] = "12.345";
  char* endptr;
  double value = std::stod(str, &endptr);

  if (errno == 0) {
    std::cout << "変換された数値: " << value << std::endl;
    std::cout << "変換が終了した文字列: " << endptr << std::endl;
  } else {
    std::cout << "エラーが発生しました: " << std::strerror(errno) << std::endl;
  }

  return 0;
}

手動変換

std::wcstold 関数を使用せずに、ワイド文字列を数値に変換することもできます。以下のコードは、その方法を示しています。

#include <iostream>

int main() {
  wchar_t str[] = L"12345";
  int value = 0;

  for (int i = 0; str[i] != '\0'; ++i) {
    value = value * 10 + str[i] - L'0';
  }

  std::cout << "変換された数値: " << value << std::endl;

  return 0;
}

第三者ライブラリ

Boost C++ Libraries や Qtなどのサードパーティライブラリには、std::wcstold 関数の代替となる関数を提供しているものがあります。これらのライブラリは、より多くの機能や柔軟性を提供する場合があります。

正規表現

ワイド文字列のパターンに一致する数値を抽出するために、正規表現を使用することができます。

#include <iostream>
#include <regex>

int main() {
  wchar_t str[] = L"The number is 12345.";
  std::wregex regex(L"[0-9]+");
  std::wsmatch match;

  if (std::regex_search(str, match, regex)) {
    std::cout << "抽出された数値: " << match[0] << std::endl;
  } else {
    std::cout << "数値が見つかりませんでした。" << std::endl;
  }

  return 0;
}

どの方法を選択するべきか

どの方法を選択するかは、状況によって異なります。

  • パターンのみに一致する数値を抽出する必要がある場合は、正規表現を使用することができます。
  • より多くの機能や柔軟性を必要とする場合は、サードパーティライブラリを使用することを検討してください。
  • ロケール設定やエラー処理が必要な場合は、std::wcstold 関数を使用する必要があります。
  • ワイド文字列を数値に変換する必要がある場合は、std::wcstold 関数または std::stod 関数が最も簡単で効率的な方法です。

上記の代替方法は、それぞれ長所と短所があります。状況に合わせて最適な方法を選択してください。