【C++チュートリアル】`std::atoll`関数を使って文字列を符号付き整数に変換する方法
構文
long long int std::atoll(const char* str);
パラメータ
str
: 変換する文字列へのポインタ
戻り値
- 変換に失敗した場合は、0
- 変換に成功した場合は、変換された
long long int
型の値
例
#include <iostream>
int main() {
char str[] = "123456789";
long long int num = std::atoll(str);
std::cout << "変換された値: " << num << std::endl;
return 0;
}
この例では、str
配列は "123456789" という文字列を格納しています。std::atoll
関数は str
に渡され、文字列を long long int
型の値 123456789 に変換します。この値は std::cout
を使用してコンソールに出力されます。
エラー処理
std::atoll
は、文字列が有効な整数表現を表していない場合、または変換された値が long long int
型の範囲外の場合、エラーを検出しません。そのため、常にエラー処理を行うことが重要です。
代替手段
std::atoll
関数は、C++11 で導入されました。それ以前のバージョンでは、atoi
関数と strtoll
関数を使用して同様の機能を実現できました。
strtoll
関数は、文字列をlong long int
型の整数値に変換し、エラー処理のための追加機能を提供します。atoi
関数は、文字列をint
型の整数値に変換します。
std::atoll
は、文字列の先頭に符号 (+/-) がある場合でも処理できます。std::atoll
は、基数 10 を使用して文字列を解釈します。std::atoll
は、文字列の先頭に空白文字がある場合でも処理できます。
文字列の符号を考慮した変換
#include <iostream>
int main() {
char str1[] = "12345";
char str2[] = "-54321";
long long int num1, num2;
num1 = std::atoll(str1);
num2 = std::atoll(str2);
std::cout << "str1 の変換結果: " << num1 << std::endl;
std::cout << "str2 の変換結果: " << num2 << std::endl;
return 0;
}
このコードでは、str1
は正の整数 "12345" を、str2
は負の整数 "-54321" を格納しています。std::atoll
関数はそれぞれの文字列を long long int
型に変換し、結果を num1
と num2
に格納します。最後に、std::cout
を使用して変換結果をコンソールに出力します。
基数を変えて変換
以下のコードは、std::atoll
関数を使用して文字列を整数に変換し、基数を変えて変換する例です。
#include <iostream>
int main() {
char str[] = "FF";
long long int num;
num = std::atoll(str, 16); // 基数 16 で変換
std::cout << "str の変換結果 (基数 16): " << num << std::endl;
return 0;
}
このコードでは、str
配列は 16 進数の文字列 "FF" を格納しています。std::atoll
関数に第二引数として 16 を渡すことで、文字列を基数 16 で解釈し、変換結果を num
に格納します。最後に、std::cout
を使用して変換結果をコンソールに出力します。
以下のコードは、std::atoll
関数を使用して文字列を整数に変換し、エラー処理を行う例です。
#include <iostream>
#include <errno.h>
int main() {
char str1[] = "123abc";
char str2[] = "2000000000000000000"; // long long int の最大値を超える値
long long int num1, num2;
num1 = std::atoll(str1);
if (errno != 0) {
std::cerr << "str1 の変換に失敗しました: " << strerror(errno) << std::endl;
} else {
std::cout << "str1 の変換結果: " << num1 << std::endl;
}
num2 = std::atoll(str2);
if (errno != 0) {
std::cerr << "str2 の変換に失敗しました: " << strerror(errno) << std::endl;
} else {
std::cout << "str2 の変換結果: " << num2 << std::endl;
}
return 0;
}
std::stoi 関数
std::stoi
関数は、C++11 で導入された関数で、文字列を int
型の整数に変換します。std::atoll
関数と同様に、文字列の先頭に空白文字がある場合でも処理できます。また、基数 10 を使用して文字列を解釈し、文字列の先頭に符号 (+/-) がある場合でも処理できます。
長所
std::atoll
関数よりも軽量です。int
型の整数のみを扱う場合に適しています。
短所
- エラー処理機能が
std::atoll
関数ほど充実していない。 long long int
型よりも小さなサイズの整数しか扱えません。
例
#include <iostream>
#include <string>
int main() {
std::string str = "12345";
int num = std::stoi(str);
std::cout << "変換結果: " << num << std::endl;
return 0;
}
std::stol 関数
長所
std::atoll
関数よりも軽量です。int
型よりも大きなサイズの整数のみを扱う場合に適しています。
短所
- エラー処理機能が
std::atoll
関数ほど充実していない。 long long int
型よりも小さなサイズの整数しか扱えません。
例
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890";
long num = std::stol(str);
std::cout << "変換結果: " << num << std::endl;
return 0;
}
std::stoll 関数
std::stoll
関数は、C++11 で導入された関数で、文字列を long long int
型の整数に変換します。std::stoi
関数と std::stol
関数と同様に、文字列の先頭に空白文字がある場合でも処理できます。また、基数 10 を使用して文字列を解釈し、文字列の先頭に符号 (+/-) がある場合でも処理できます。
長所
std::atoll
関数と同様のエラー処理機能を提供します。long long int
型の整数変換に最適です。
短所
std::stoi
関数とstd::stol
関数よりも重い処理となります。
例
#include <iostream>
#include <string>
int main() {
std::string str = "9223372036854775807"; // long long int の最大値
long long int num = std::stoll(str);
std::cout << "変換結果: " << num << std::endl;
return 0;
}
手動による変換
単純な文字列変換の場合、手動で変換を行うことも可能です。以下の例は、文字列を int
型の整数に変換する方法を示しています。
#include <iostream>
#include <string>
int main() {
std::string str = "12345";
int num = 0;
for (char c : str) {
num = num * 10 + (c - '0');
}
std::cout << "変換結果: " << num << std::endl;
return 0;
}
この方法は、比較的単純なケースでのみ使用することをお勧めします。複雑なケースや、エラー処理が必要な場合は、上記の標準ライブラリ関数を使用する方が効率的です。