数値から文字列

std::to_string

#include <string>
 
int n = 42;
std::string s = std::to_string(n);  // "42"
double d = 3.14;
std::string s = std::to_string(d);  // "3.140000"

文字列から数値

std::stox

引数にとる文字列を数値型へ変換する関数が、各数値型ごとにstoxのような形式の命名で実装されている。

変換できない場合は例外std::invalid_argumentが送出されるため処理を考慮する。

例:

try {
    int n = std::stoi("abc");
} catch (const std::invalid_argument& e) {
    // 数値でない時の処理
}

関連

参考