std::string を変換
数値型への変換
std::string → int, short, long, long long, unsigned int, unsigned short, unsigned long
![](https://lets-programing.com/wp-content/uploads/2024/04/image-28.png)
C++11以降ではstd::stoi
(またはstd::stol
, std::stoll
など)を使うことも可能
![](https://lets-programing.com/wp-content/uploads/2024/04/image-29.png)
std::string → float, double, long double
![](https://lets-programing.com/wp-content/uploads/2024/04/image-30.png)
文字型への変換
std::string → char
に変換する
![](https://lets-programing.com/wp-content/uploads/2024/04/image-36.png)
std::string → char[]
に変換する
![](https://lets-programing.com/wp-content/uploads/2024/04/image-37.png)
std::string → unsigned char[]
に変換する
![](https://lets-programing.com/wp-content/uploads/2024/04/image-38.png)
std::string → CString
に変換する
![](https://lets-programing.com/wp-content/uploads/2024/04/image-39.png)
ブール型への変換
![](https://lets-programing.com/wp-content/uploads/2024/04/image-32.png)
固定幅整数型
std::stoi
関数、std::stol
関数、std::stoll
関数で固定幅の数値を変換できます。
下の例では、std::stoll
を使用して、入力文字列を long long
型に変換します。
これにより、ほとんどの整数型に対応できる最大範囲を確保します。
![](https://lets-programing.com/wp-content/uploads/2024/04/image-33.png)
![](https://lets-programing.com/wp-content/uploads/2024/04/image-34.png)
![](https://lets-programing.com/wp-content/uploads/2024/04/image-35.png)