一、基础输入输出(I/O)
1. <iostream>
-
核心作用:提供 C++ 标准的输入输出流功能,是控制台程序最基础的头文件。
-
核心组件 :
cin(标准输入,读取键盘输入)、cout(标准输出,打印到控制台)、cerr(标准错误输出)、clog(日志输出)。 -
示例:
#include <iostream>
using namespace std; // 简化命名空间,新手常用int main() {
string name;
cout << "请输入你的名字:"; // 输出提示
cin >> name; // 读取输入
cout << "你好," << name << "!" << endl; // 输出结果
return 0;
}
二、数据类型与通用工具
1. <string>
-
核心作用 :提供 C++ 风格的
std::string字符串类,替代 C 语言的字符数组,功能更强大、更安全。 -
核心功能:字符串拼接、长度获取、比较、查找、替换等。
-
示例:
#include <iostream>
#include <string>
using namespace std;int main() {
string str1 = "Hello";
string str2 = " World";
string str3 = str1 + str2; // 拼接字符串
cout << "拼接结果:" << str3 << endl; // 输出 Hello World
cout << "字符串长度:" << str3.size() << endl; // 输出 11
return 0;
}
2. <vector>
-
核心作用 :提供动态数组(向量)
std::vector,是 C++ 中最常用的容器,可自动扩容,替代 C 语言的静态数组。 -
核心功能:动态添加 / 删除元素、随机访问、获取长度、清空等。
-
示例:
#include <iostream>
#include <vector>
using namespace std;int main() {
vector<int> nums; // 定义一个存储int的动态数组
nums.push_back(10); // 添加元素
nums.push_back(20);
nums.push_back(30);// 遍历数组 for (int num : nums) { cout << num << " "; // 输出 10 20 30 } return 0;}
3. <algorithm>
-
核心作用:提供大量通用的算法函数,如排序、查找、交换、反转等,配合容器使用。
-
核心函数 :
sort()(排序)、find()(查找)、reverse()(反转)、swap()(交换)。 -
示例:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {
vector<int> nums = {3, 1, 4, 2};
sort(nums.begin(), nums.end()); // 升序排序cout << "排序后:"; for (int num : nums) { cout << num << " "; // 输出 1 2 3 4 } // 查找元素4 auto it = find(nums.begin(), nums.end(), 4); if (it != nums.end()) { cout << "\n找到元素4,位置:" << it - nums.begin() << endl; // 输出 3 } return 0;}
4. <cstdlib>
-
核心作用 :提供通用的实用函数,如内存分配、随机数、程序退出、数值转换等(C++ 兼容 C 的
<stdlib.h>)。 -
核心函数 :
rand()(生成随机数)、srand()(设置随机数种子)、exit()(退出程序)、atoi()(字符串转 int)。 -
示例:
#include <iostream>
#include <cstdlib>
#include <ctime> // 用于设置随机数种子
using namespace std;int main() {
srand(time(0)); // 以当前时间为种子,避免随机数重复
int random_num = rand() % 100; // 生成0-99的随机数
cout << "随机数:" << random_num << endl;string str = "123"; int num = atoi(str.c_str()); // 字符串转int cout << "转换后:" << num + 1 << endl; // 输出 124 return 0;}
三、日期与时间
1. <ctime>
-
核心作用 :提供日期和时间相关的函数,如获取当前时间、时间转换、计时等(兼容 C 的
<time.h>)。 -
核心函数 :
time()(获取当前时间戳)、localtime()(转换为本地时间)、clock()(程序运行时间)。 -
示例:
#include <iostream>
#include <ctime>
using namespace std;int main() {
// 获取当前时间
time_t now = time(0);
tm* local_time = localtime(&now);
cout << "当前时间:" << 1900 + local_time->tm_year << "-"
<< 1 + local_time->tm_mon << "-"
<< local_time->tm_mday << " "
<< local_time->tm_hour << ":"
<< local_time->tm_min << ":"
<< local_time->tm_sec << endl;// 计时示例 clock_t start = clock(); // 模拟耗时操作 for (int i = 0; i < 100000000; i++); clock_t end = clock(); double duration = (double)(end - start) / CLOCKS_PER_SEC; cout << "耗时:" << duration << " 秒" << endl; return 0;}
四、数学运算
1. <cmath>
-
核心作用 :提供各种数学运算函数,如三角函数、指数、对数、绝对值、取整等(兼容 C 的
<math.h>)。 -
核心函数 :
sqrt()(平方根)、pow()(幂运算)、abs()(绝对值)、sin()/cos()(三角函数)、round()(四舍五入)。 -
示例:
#include <iostream>
#include <cmath>
using namespace std;int main() {
double num = 16.0;
cout << "16的平方根:" << sqrt(num) << endl; // 输出 4double base = 2, exp = 3; cout << "2^3 = " << pow(base, exp) << endl; // 输出 8 double neg = -3.14; cout << "-3.14的绝对值:" << abs(neg) << endl; // 输出 3.14 return 0;}
五、文件操作
1. <fstream>
-
核心作用:提供文件的输入输出功能,支持读写文本文件 / 二进制文件。
-
核心组件 :
ifstream(读取文件)、ofstream(写入文件)、fstream(读写文件)。 -
示例:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;int main() {
// 写入文件
ofstream outfile("test.txt");
if (outfile.is_open()) {
outfile << "Hello, File!" << endl;
outfile << "C++ 文件操作示例" << endl;
outfile.close();
}// 读取文件 ifstream infile("test.txt"); string line; if (infile.is_open()) { cout << "文件内容:" << endl; while (getline(infile, line)) { // 逐行读取 cout << line << endl; } infile.close(); } else { cout << "无法打开文件" << endl; } return 0;}
六、其他常用头文件
| 头文件 | 核心作用 | 核心组件 / 函数 |
|---|---|---|
<cstdio> |
C 风格输入输出(兼容<stdio.h>) |
printf()、scanf()、fopen() |
<vector> |
动态数组(最常用容器) | push_back()、size()、begin() |
<map> |
键值对映射(有序) | std::map、insert()、find() |
<unordered_map> |
哈希表(无序,查询更快) | std::unordered_map |
<set> |
有序不重复集合 | std::set、insert()、erase() |
<utility> |
通用工具,如键值对pair |
std::pair、swap() |
<limits> |
各种数据类型的取值范围 | numeric_limits<int>::max() |
总结
- 基础必备 :
<iostream>(输入输出)、<string>(字符串)、<vector>(动态数组)是 C++ 入门最核心的头文件,几乎所有程序都会用到。 - 功能扩展 :
<algorithm>(算法)、<cmath>(数学)、<ctime>(时间)、<cstdlib>(通用工具)用于满足特定功能需求。 - 文件操作 :
<fstream>是处理文件读写的核心,替代 C 风格的<cstdio>文件操作更符合 C++ 特性。
记住这些头文件的核心用途,能让你在编写 C++ 程序时快速选择合适的头文件,避免重复造轮子,同时遵循 C++ 的最佳实践。