C++頭文件的具體使用
一、標(biāo)準(zhǔn)C++頭文件(無擴(kuò)展名)
核心語言功能
#include <iostream> // 輸入輸出流(cin, cout, cerr, clog) #include <iomanip> // 格式化輸出(setw, setprecision等) #include <fstream> // 文件流(ifstream, ofstream) #include <sstream> // 字符串流(istringstream, ostringstream) #include <string> // 字符串類(std::string)
容器類
#include <vector> // 動(dòng)態(tài)數(shù)組 #include <list> // 雙向鏈表 #include <deque> // 雙端隊(duì)列 #include <array> // 固定大小數(shù)組(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> // 隊(duì)列(queue, priority_queue) #include <bitset> // 位集
算法和數(shù)值操作
#include <algorithm> // 常用算法(sort, find, transform等) #include <numeric> // 數(shù)值算法(accumulate, inner_product等) #include <functional> // 函數(shù)對象和函數(shù)適配器 #include <cmath> // 數(shù)學(xué)函數(shù) #include <complex> // 復(fù)數(shù)運(yùn)算 #include <random> // 隨機(jī)數(shù)生成(C++11) #include <limits> // 數(shù)值極限(numeric_limits)
內(nèi)存管理
#include <memory> // 智能指針(unique_ptr, shared_ptr等) #include <new> // 動(dòng)態(tài)內(nèi)存操作
異常處理
#include <exception> // 異常基類 #include <stdexcept> // 標(biāo)準(zhǔn)異常類
類型支持
#include <typeinfo> // 類型信息(typeid) #include <type_traits> // 類型特性(C++11) #include <cstddef> // 常用類型定義(size_t, nullptr_t等) #include <cstdint> // 固定寬度整數(shù)類型(C++11)
多線程支持(C++11及以后)
#include <thread> // 線程類 #include <mutex> // 互斥鎖 #include <atomic> // 原子操作 #include <condition_variable> // 條件變量 #include <future> // 異步操作
二、標(biāo)準(zhǔn)C頭文件(c前綴)
這些頭文件源自C語言,在C++中為了兼容而保留:
#include <cstdio> // 標(biāo)準(zhǔn)輸入輸出(printf, scanf等) #include <cstdlib> // 通用工具(malloc, free, rand, exit等) #include <cstring> // 字符串處理(strcpy, strlen, memcpy等) #include <ctime> // 時(shí)間日期函數(shù) #include <cctype> // 字符分類函數(shù)(isalpha, isdigit等) #include <cmath> // 數(shù)學(xué)函數(shù)(C++中也可用<cmath>替代<math.h>) #include <cassert> // 斷言宏 #include <cerrno> // 錯(cuò)誤號(hào)
三、常用特殊用途頭文件
#include <utility> // 工具函數(shù)(pair, move, forward等) #include <tuple> // 元組(C++11) #include <regex> // 正則表達(dá)式(C++11) #include <chrono> // 時(shí)間庫(C++11) #include <ratio> // 編譯期有理數(shù)(C++11) #include <initializer_list> // 初始化列表(C++11) #include <filesystem> // 文件系統(tǒng)(C++17) #include <any> // 任意類型容器(C++17) #include <variant> // 類型安全的聯(lián)合體(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;
}五、最佳實(shí)踐
使用標(biāo)準(zhǔn)頭文件:優(yōu)先使用C++標(biāo)準(zhǔn)頭文件而非C風(fēng)格頭文件
包含守衛(wèi):自定義頭文件應(yīng)使用包含守衛(wèi)防止重復(fù)包含
#ifndef MY_HEADER_H #define MY_HEADER_H // 頭文件內(nèi)容 #endif
使用pragma once(非標(biāo)準(zhǔn)但廣泛支持)
#pragma once // 頭文件內(nèi)容
避免包含不必要頭文件:只包含實(shí)際需要的頭文件,減少編譯時(shí)間
使用前向聲明:當(dāng)只需要指針或引用時(shí),使用前向聲明而非包含整個(gè)頭文件
六、C++20新增頭文件
#include <compare> // 三路比較運(yùn)算符(<=>) #include <concepts> // 概念(concepts) #include <ranges> // 范圍(ranges) #include <span> // 連續(xù)序列視圖(span) #include <bit> // 位操作 #include <numbers> // 數(shù)學(xué)常數(shù) #include <syncstream> // 同步輸出流 #include <stop_token> // 停止令牌
到此這篇關(guān)于C++頭文件的具體使用的文章就介紹到這了,更多相關(guān)C++頭文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)哈夫曼樹簡單創(chuàng)建與遍歷的方法
這篇文章主要介紹了C++實(shí)現(xiàn)哈夫曼樹簡單創(chuàng)建與遍歷的方法,對于C++算法的學(xué)習(xí)來說不失為一個(gè)很好的借鑒實(shí)例,需要的朋友可以參考下2014-07-07
C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開發(fā))
這篇文章主要介紹了C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開發(fā)),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Qt基礎(chǔ)開發(fā)之QString與QByteArray詳細(xì)用法與區(qū)別及QString QByteArray互轉(zhuǎn)
這篇文章主要介紹了Qt基礎(chǔ)開發(fā)之QString與QByteArray詳細(xì)用法與區(qū)別及QString QByteArray互轉(zhuǎn),需要的朋友可以參考下2020-03-03

