C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++头文件

C++头文件的具体使用

作者:忘川渡梦

本文主要介绍了C++头文件的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、标准C++头文件(无扩展名)

核心语言功能

#include <iostream>      // 输入输出流(cin, cout, cerr, clog)
#include <iomanip>       // 格式化输出(setw, setprecision等)
#include <fstream>       // 文件流(ifstream, ofstream)
#include <sstream>       // 字符串流(istringstream, ostringstream)
#include <string>        // 字符串类(std::string)

容器类

#include <vector>        // 动态数组
#include <list>          // 双向链表
#include <deque>         // 双端队列
#include <array>         // 固定大小数组(C++11)
#include <forward_list>  // 单向链表(C++11)
#include <set>           // 有序集合
#include <map>           // 有序映射
#include <unordered_set> // 哈希集合(C++11)
#include <unordered_map> // 哈希映射(C++11)
#include <stack>         // 栈
#include <queue>         // 队列(queue, priority_queue)
#include <bitset>        // 位集

算法和数值操作

#include <algorithm>     // 常用算法(sort, find, transform等)
#include <numeric>       // 数值算法(accumulate, inner_product等)
#include <functional>    // 函数对象和函数适配器
#include <cmath>         // 数学函数
#include <complex>       // 复数运算
#include <random>        // 随机数生成(C++11)
#include <limits>        // 数值极限(numeric_limits)

内存管理

#include <memory>        // 智能指针(unique_ptr, shared_ptr等)
#include <new>           // 动态内存操作

异常处理

#include <exception>     // 异常基类
#include <stdexcept>     // 标准异常类

类型支持

#include <typeinfo>      // 类型信息(typeid)
#include <type_traits>   // 类型特性(C++11)
#include <cstddef>       // 常用类型定义(size_t, nullptr_t等)
#include <cstdint>       // 固定宽度整数类型(C++11)

多线程支持(C++11及以后)

#include <thread>        // 线程类
#include <mutex>         // 互斥锁
#include <atomic>        // 原子操作
#include <condition_variable> // 条件变量
#include <future>        // 异步操作

二、标准C头文件(c前缀)

这些头文件源自C语言,在C++中为了兼容而保留:

#include <cstdio>        // 标准输入输出(printf, scanf等)
#include <cstdlib>       // 通用工具(malloc, free, rand, exit等)
#include <cstring>       // 字符串处理(strcpy, strlen, memcpy等)
#include <ctime>         // 时间日期函数
#include <cctype>        // 字符分类函数(isalpha, isdigit等)
#include <cmath>         // 数学函数(C++中也可用<cmath>替代<math.h>)
#include <cassert>       // 断言宏
#include <cerrno>        // 错误号

三、常用特殊用途头文件

#include <utility>       // 工具函数(pair, move, forward等)
#include <tuple>         // 元组(C++11)
#include <regex>         // 正则表达式(C++11)
#include <chrono>        // 时间库(C++11)
#include <ratio>         // 编译期有理数(C++11)
#include <initializer_list> // 初始化列表(C++11)
#include <filesystem>    // 文件系统(C++17)
#include <any>           // 任意类型容器(C++17)
#include <variant>       // 类型安全的联合体(C++17)
#include <optional>      // 可选值(C++17)

四、头文件使用示例

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() {
    std::vector<int> vec = {5, 2, 8, 1, 9};
    
    // 使用algorithm排序
    std::sort(vec.begin(), vec.end());
    
    // 使用iostream输出
    for (int num : vec) {
        std::cout << num << " ";
    }
    
    return 0;
}

五、最佳实践

使用标准头文件:优先使用C++标准头文件而非C风格头文件

包含守卫:自定义头文件应使用包含守卫防止重复包含

#ifndef MY_HEADER_H
#define MY_HEADER_H
// 头文件内容
#endif

使用pragma once(非标准但广泛支持)

#pragma once
// 头文件内容

避免包含不必要头文件:只包含实际需要的头文件,减少编译时间

使用前向声明:当只需要指针或引用时,使用前向声明而非包含整个头文件

六、C++20新增头文件

#include <compare>        // 三路比较运算符(<=>)
#include <concepts>       // 概念(concepts)
#include <ranges>         // 范围(ranges)
#include <span>           // 连续序列视图(span)
#include <bit>           // 位操作
#include <numbers>       // 数学常数
#include <syncstream>    // 同步输出流
#include <stop_token>    // 停止令牌

到此这篇关于C++头文件的具体使用的文章就介绍到这了,更多相关C++头文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文