C++ 標準ライブラリ <cstdlib>
<cstdlib>これは C++ 標準ライブラリのヘッダーファイルであり、メモリ割り当て、プロセス制御、環境クエリ、ソートと検索、数学変換、擬似乱数生成など、さまざまな汎用ユーティリティ関数を提供します。これらの関数はもともと C 標準ライブラリに由来し、<stdlib.h>C++ で標準化および拡張されました。
構文
C++ プログラムでは、cstdlibの関数を使用するには、最初にこのヘッダーファイルを含める必要があります:
#include <cstdlib>
よく使う関数
cstdlibには多くの便利な関数が含まれており、以下は一般的な関数とその簡単な説明です:
exit(int status): プログラムの実行を終了し、ステータスコードを返します。system(const char* command): コマンドライン文字列を実行します。malloc(size_t size): 指定されたサイズのメモリを割り当てます。free(void* ptr): 以前に割り当てられたメモリを解放します。atoi(const char* str): 文字列を整数に変換します。atof(const char* str): 文字列を浮動小数点数に変換します。rand(): 乱数を生成します。srand(unsigned int seed): 乱数生成器のシードを設定します。
例
以下は、cstdlibの関数を使用した例です:
例1:exit関数の使用
例
#include <iostream>
#include <cstdlib>
int main() {
std::cout << "This program will exit now." << std::endl;
exit(0); // プログラムを正常に終了
return 0; // この行のコードは実行されません
}
#include <cstdlib>
int main() {
std::cout << "This program will exit now." << std::endl;
exit(0); // プログラムを正常に終了
return 0; // この行のコードは実行されません
}
出力結果:
This program will exit now.
例2:system関数の使用
例
#include <iostream>
#include <cstdlib>
int main() {
std::cout << "Executing a system command: dir" << std::endl;
system("dir"); // Windowsで現在のディレクトリのファイルとフォルダを表示
return 0;
}
#include <cstdlib>
int main() {
std::cout << "Executing a system command: dir" << std::endl;
system("dir"); // Windowsで現在のディレクトリのファイルとフォルダを表示
return 0;
}
出力結果:
Executing a system command: dir
その後、現在のディレクトリのファイルとフォルダの一覧が表示されます。
例3:mallocとfree関数の使用
例
#include <iostream>
#include <cstdlib>
int main() {
int* ptr = (int*)malloc(10 * sizeof(int)); // メモリを割り当てる
if (ptr == NULL) {
std::cout << "Memory allocation failed." << std::endl;
return 1;
}
for (int i = 0; i < 10; ++i) {
ptr[i] = i * i; // 割り当てたメモリを使用する
}
for (int i = 0; i < 10; ++i) {
std::cout << "Element " << i << ": " << ptr[i] << std::endl;
}
free(ptr); // メモリを解放する
return 0;
}
#include <cstdlib>
int main() {
int* ptr = (int*)malloc(10 * sizeof(int)); // メモリを割り当てる
if (ptr == NULL) {
std::cout << "Memory allocation failed." << std::endl;
return 1;
}
for (int i = 0; i < 10; ++i) {
ptr[i] = i * i; // 割り当てたメモリを使用する
}
for (int i = 0; i < 10; ++i) {
std::cout << "Element " << i << ": " << ptr[i] << std::endl;
}
free(ptr); // メモリを解放する
return 0;
}
出力結果:
Element 0: 0 Element 1: 1 Element 2: 4 ... Element 9: 81
例4:atoiとatof関数の使用
例
#include <iostream>
#include <cstdlib>
int main() {
std::string str1 = "123";
std::string str2 = "456.78";
int num1 = std::atoi(str1.c_str()); // 文字列を整数に変換する
double num2 = std::atof(str2.c_str()); // 文字列を浮動小数点数に変換する
std::cout << "Integer: " << num1 << std::endl;
std::cout << "Float: " << num2 << std::endl;
return 0;
}
#include <cstdlib>
int main() {
std::string str1 = "123";
std::string str2 = "456.78";
int num1 = std::atoi(str1.c_str()); // 文字列を整数に変換する
double num2 = std::atof(str2.c_str()); // 文字列を浮動小数点数に変換する
std::cout << "Integer: " << num1 << std::endl;
std::cout << "Float: " << num2 << std::endl;
return 0;
}
出力結果:
Integer: 123 Float: 456.78
例5:randとsrand関数の使用
例
#include <cstdlib>
#include <iostream>
#include <ctime>
int main() {
std::srand(std::time(nullptr)); // 現在の時刻を乱数シードとして使用する
for (int i = 0; i < 5; ++i) {
std::cout << std::rand() % 100 << " "; // 0から99の間の乱数を生成する
}
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <ctime>
int main() {
std::srand(std::time(nullptr)); // 現在の時刻を乱数シードとして使用する
for (int i = 0; i < 5; ++i) {
std::cout << std::rand() % 100 << " "; // 0から99の間の乱数を生成する
}
std::cout << std::endl;
return 0;
}
出力結果:
53 22 62 68 62その他の拡張