關(guān)于我們
書單推薦
新書推薦
|
C++之旅(第3版) 讀者對(duì)象:有其他語言編程經(jīng)驗(yàn),想要了解C++語言的讀者;有傳統(tǒng)C++編程經(jīng)驗(yàn),想要了解現(xiàn)代C++語言特性的讀者;有較豐富編程經(jīng)驗(yàn)且想了解C++之父在C++設(shè)計(jì)過程中的一些設(shè)計(jì)細(xì)節(jié)與思路的讀者。
本書一共19章,以C++20為標(biāo)準(zhǔn),講述了最新的現(xiàn)代C++所提供的編程特性。有其他語言編程經(jīng)驗(yàn)的讀者可以從本書中快速了解C++所具備的功能,從而獲得對(duì)現(xiàn)代C++的更全面認(rèn)知,以便更好地了解現(xiàn)代C++語言已經(jīng)發(fā)展到的程度。資深程序員可以從本書作者的整體行文風(fēng)格中感受到他在設(shè)計(jì)C++特性時(shí)的一些考量及側(cè)重點(diǎn),了解C++這門語言在歷史上曾經(jīng)歷過的變遷,以及一部分特性為什么會(huì)是今天這個(gè)樣子。所以,本書適合的讀者:有其他語言編程經(jīng)驗(yàn),想要了解C++語言的讀者;有傳統(tǒng)C++編程經(jīng)驗(yàn),想要了解現(xiàn)代C++語言特性的讀者;有較豐富編程經(jīng)驗(yàn)且想了解C++之父在C++設(shè)計(jì)過程中的一些設(shè)計(jì)細(xì)節(jié)與思路的讀者。
Stroustrup 是 C++ 的設(shè)計(jì)者和原始實(shí)現(xiàn)者,著有《C++程序設(shè)計(jì)原理與實(shí)踐》(第2版)和《C++程序設(shè)計(jì)語言》(第4版)等圖書。他目前是哥倫比亞大學(xué)的教授,之前曾在 AT&T 貝爾實(shí)驗(yàn)室、得克薩斯 A&M 大學(xué)和摩根士丹利工作。他獲得了無數(shù)榮譽(yù),包括美國國家工程院的查爾斯·斯塔克·德雷珀獎(jiǎng),以表彰其開發(fā)和改進(jìn) C++ 編程語言。Stroustrup 博士是美國國家工程院院士,也是 IEEE、ACM、CHM 和劍橋丘吉爾學(xué)院的院士。
譯者pansz是一個(gè)經(jīng)驗(yàn)豐富的軟件工程師,從小學(xué)開始學(xué)習(xí)編程,中學(xué)時(shí)期已成為高級(jí)程序員,迄今擁有三十年以上編程經(jīng)驗(yàn),對(duì)包含 C++ 在內(nèi)的多種主流編程語言有著長(zhǎng)期的深入了解,并參與制定了其供職的多家企業(yè)的編程規(guī)范。
第 1 章 基礎(chǔ)............................................................................................................................1
1.1 引言 ...............................................................................................................................1 1.2 程序 ...............................................................................................................................2 1.2.1 Hello, World!................................................................................................................. 2 1.3 函數(shù) ...............................................................................................................................4 1.4 類型、變量與運(yùn)算........................................................................................................6 1.4.1 算術(shù)運(yùn)算 ....................................................................................................................... 7 1.4.2 初始化........................................................................................................................... 9 1.5 作用域和生命周期...................................................................................................... 10 1.6 常量 ............................................................................................................................. 11 1.7 指針、數(shù)組和引用...................................................................................................... 13 1.7.1 空指針..........................................................................................................................15 1.8 檢驗(yàn) ............................................................................................................................. 16 1.9 映射到硬件 ................................................................................................................. 19 1.9.1 賦值..............................................................................................................................19 1.9.2 初始化..........................................................................................................................20 1.10 建議 ........................................................................................................................... 21 第 2 章 用戶自定義類型........................................................................................................23 2.1 引言 ............................................................................................................................. 23 2.2 結(jié)構(gòu) ............................................................................................................................. 24 2.3 類 ................................................................................................................................. 25 2.4 枚舉 ............................................................................................................................. 27 2.5 聯(lián)合 ............................................................................................................................. 29 2.6 建議 ............................................................................................................................. 31 第 3 章 模塊化 ......................................................................................................................32 3.1 引言 ............................................................................................................................. 32 3.2 分離編譯 ..................................................................................................................... 33 3.2.1 頭文件..........................................................................................................................34 3.2.2 模塊..............................................................................................................................36 3.3 命名空間 ..................................................................................................................... 39 3.4 函數(shù)參數(shù)與返回值...................................................................................................... 41 3.4.1 參數(shù)傳遞 ......................................................................................................................42 3.4.2 返回值..........................................................................................................................43 3.4.3 返回類型推導(dǎo) ..............................................................................................................45 3.4.4 返回類型后置 ..............................................................................................................45 3.4.5 結(jié)構(gòu)化綁定 ..................................................................................................................46 3.5 建議 ............................................................................................................................. 47 第 4 章 錯(cuò)誤處理...................................................................................................................48 4.1 引言 ............................................................................................................................. 48 4.2 異常 ............................................................................................................................. 49 4.3 約束條件 ..................................................................................................................... 50 4.4 錯(cuò)誤處理的其他替代方式.......................................................................................... 52 4.5 斷言 ............................................................................................................................. 54 4.5.1 assert() ..........................................................................................................................55 4.5.2 static_assert...................................................................................................................56 4.5.3 noexcept........................................................................................................................56 4.6 建議 ............................................................................................................................. 57 第 5 章 類..............................................................................................................................58 5.1 引言 ............................................................................................................................. 58 5.1.1 類的概述 ......................................................................................................................59 5.2 具體類型 ..................................................................................................................... 59 5.2.1 一種算術(shù)類型 ..............................................................................................................60 5.2.2 容器..............................................................................................................................62 5.2.3 容器的初始化 ..............................................................................................................64 5.3 抽象類型 ..................................................................................................................... 66 5.4 虛函數(shù) ......................................................................................................................... 69 5.5 類層次結(jié)構(gòu) ................................................................................................................. 70 5.5.1 類層次結(jié)構(gòu)的益處 ......................................................................................................72 5.5.2 類層次結(jié)構(gòu)導(dǎo)航 ..........................................................................................................74 5.5.3 避免資源泄漏 ..............................................................................................................75 5.6 建議 ............................................................................................................................. 76 第 6 章 基本操作...................................................................................................................78 6.1 引言 ............................................................................................................................. 78 6.1.1 基本操作 ......................................................................................................................79 6.1.2 轉(zhuǎn)換..............................................................................................................................81 6.1.3 成員初始值設(shè)定項(xiàng) ......................................................................................................81 6.2 拷貝和移動(dòng) ................................................................................................................. 82 6.2.1 拷貝容器 ......................................................................................................................82 6.2.2 移動(dòng)容器 ......................................................................................................................84 6.3 資源管理 ..................................................................................................................... 87 6.4 操作符重載 ................................................................................................................. 88 6.5 常規(guī)操作 ..................................................................................................................... 89 6.5.1 比較(關(guān)系操作符) ..................................................................................................90 6.5.2 容器操作 ......................................................................................................................92 6.5.3 迭代器及智能指針 ......................................................................................................92 6.5.4 輸入與輸出操作 ..........................................................................................................93 6.5.5 swap() ...........................................................................................................................93 6.5.6 hash<> ..........................................................................................................................93 6.6 用戶自定義字面量...................................................................................................... 93 6.7 建議 ............................................................................................................................. 94 第 7 章 模板..........................................................................................................................96 7.1 引言 ............................................................................................................................. 96 7.2 參數(shù)化類型 ................................................................................................................. 97 7.2.1 受限模板參數(shù) ..............................................................................................................99 7.2.2 模板值參數(shù) ................................................................................................................100 7.2.3 模板參數(shù)推導(dǎo) ............................................................................................................100 7.3 參數(shù)化操作 ............................................................................................................... 102 7.3.1 模板函數(shù) ....................................................................................................................103 7.3.2 函數(shù)對(duì)象 ....................................................................................................................103 7.3.3 匿名函數(shù)表達(dá)式 ........................................................................................................105 7.4 模板機(jī)制 ................................................................................................................... 109 7.4.1 模板變量 ....................................................................................................................109 7.4.2 別名............................................................................................................................110 7.4.3 編譯時(shí) if.....................................................................................................................111 7.5 建議 ........................................................................................................................... 113 第 8 章 概念和泛型編程......................................................................................................114 8.1 引言 ........................................................................................................................... 114 8.2 概念 ........................................................................................................................... 115 8.2.1 概念的運(yùn)用 ................................................................................................................116 8.2.2 基于概念的重載 ........................................................................................................117 8.2.3 有效代碼 ....................................................................................................................118 8.2.4 定義概念 ....................................................................................................................119 8.2.5 概念與 auto ................................................................................................................122 8.2.6 類型與概念 ................................................................................................................124 8.3 泛型編程 ................................................................................................................... 124 8.3.1 概念的使用 ................................................................................................................124 8.3.2 使用模板實(shí)現(xiàn)抽象 ....................................................................................................125 8.4 可變參數(shù)模板 ........................................................................................................... 127 8.4.1 折疊表達(dá)式 ................................................................................................................128 8.4.2 完美轉(zhuǎn)發(fā)參數(shù) ............................................................................................................129 8.5 模板編譯模型 ........................................................................................................... 130 8.6 建議 ........................................................................................................................... 131 第 9 章 標(biāo)準(zhǔn)庫 ....................................................................................................................132 9.1 引言 ........................................................................................................................... 132 9.2 標(biāo)準(zhǔn)庫組件 ............................................................................................................... 133 9.3 標(biāo)準(zhǔn)庫的組織 ........................................................................................................... 134 9.3.1 命名空間 ....................................................................................................................134 9.3.2 ranges 命名空間.........................................................................................................135 9.3.3 模塊............................................................................................................................136 9.3.4 頭文件........................................................................................................................136 9.4 建議 ........................................................................................................................... 138 第 10 章 字符串和正則表達(dá)式.............................................................................................139 10.1 引言 ......................................................................................................................... 139 10.2 字符串 ..................................................................................................................... 139 10.2.1 string 的實(shí)現(xiàn) ............................................................................................................142 10.3 字符串視圖 ............................................................................................................. 142 10.4 正則表達(dá)式 ............................................................................................................. 144 10.4.1 搜索..........................................................................................................................145 10.4.2 正則表達(dá)式的符號(hào)表示...........................................................................................146 10.4.3 迭代器......................................................................................................................151 10.5 建議 ......................................................................................................................... 151 第 11 章 輸入和輸出 ...........................................................................................................153 11.1 引言 ......................................................................................................................... 153 11.2 輸出 ......................................................................................................................... 154 11.3 輸入 ......................................................................................................................... 156 11.4 I/O 狀態(tài) ................................................................................................................... 157 11.5 用戶自定義類型的 I/O ........................................................................................... 158 11.6 輸出格式化 ............................................................................................................. 159 11.6.1 流式格式化 ..............................................................................................................160 11.6.2 printf()風(fēng)格的格式化...............................................................................................161 11.7 流 ............................................................................................................................. 164 11.7.1 標(biāo)準(zhǔn)流......................................................................................................................164 11.7.2 文件流......................................................................................................................165 11.7.3 字符串流 ..................................................................................................................165 11.7.4 內(nèi)存流......................................................................................................................166 11.7.5 同步流......................................................................................................................167 11.8 C 風(fēng)格的 I/O............................................................................................................ 168 11.9 文件系統(tǒng) ................................................................................................................. 168 11.9.1 路徑..........................................................................................................................168 11.9.2 文件和目錄 ..............................................................................................................171 11.10 建議 ....................................................................................................................... 172 第 12 章 容器......................................................................................................................174 12.1 引言 ......................................................................................................................... 174 12.2 vector ....................................................................................................................... 175 12.2.1 元素..........................................................................................................................178 12.2.2 范圍檢查 ..................................................................................................................178 12.3 list ............................................................................................................................ 180 12.4 forward_list.............................................................................................................. 181 12.5 map .......................................................................................................................... 182 12.6 unordered_map ........................................................................................................ 183 12.7 分配器 ..................................................................................................................... 184 12.8 容器概述 ................................................................................................................. 186 12.9 建議 ......................................................................................................................... 188 第 13 章 算法......................................................................................................................190 13.1 引言 ......................................................................................................................... 190 13.2 使用迭代器 ............................................................................................................. 192 13.3 迭代器類型 ............................................................................................................. 195 13.3.1 流迭代器 ..................................................................................................................196 13.4 使用謂詞 ................................................................................................................. 199 13.5 標(biāo)準(zhǔn)庫算法概覽 ..................................................................................................... 199 13.6 并行算法 ................................................................................................................. 201 13.7 建議 ......................................................................................................................... 202 第 14 章 范圍......................................................................................................................203 14.1 引言 ......................................................................................................................... 203 14.2 視圖 ......................................................................................................................... 204 14.3 生成器 ..................................................................................................................... 206 14.4 管道 ......................................................................................................................... 207 14.5 概念概述 ................................................................................................................. 208 14.5.1 類型概念 ..................................................................................................................209 14.5.2 迭代器概念 ..............................................................................................................211 14.5.3 范圍概念 ..................................................................................................................212 14.6 建議 ......................................................................................................................... 213 第 15 章 指針和容器 ...........................................................................................................214 15.1 引言 ......................................................................................................................... 214 15.2 指針類型 ................................................................................................................. 215 15.2.1 unique_ptr 及 shared_ptr...........................................................................................216 15.2.2 span...........................................................................................................................219 15.3 容器 ......................................................................................................................... 221 15.3.1 array..........................................................................................................................222 15.3.2 bitset .........................................................................................................................224 15.3.3 pair............................................................................................................................225 15.3.4 tuple ..........................................................................................................................227 15.4 可變類型容器 ......................................................................................................... 229 15.4.1 variant.......................................................................................................................229 15.4.2 optional.....................................................................................................................231 15.4.3 any ............................................................................................................................232 15.5 建議 ......................................................................................................................... 233 第 16 章 實(shí)用工具...............................................................................................................234 16.1 引言 ......................................................................................................................... 234 16.2 時(shí)間 ......................................................................................................................... 235 16.2.1 時(shí)鐘..........................................................................................................................235 16.2.2 日歷..........................................................................................................................236 16.2.3 時(shí)區(qū)..........................................................................................................................237 16.3 函數(shù)適配 ................................................................................................................. 237 16.3.1 匿名函數(shù)作為適配器 ..............................................................................................238 16.3.2 mem_fn() ..................................................................................................................238 16.3.3 function.....................................................................................................................238 16.4 類型函數(shù) ................................................................................................................. 239 16.4.1 類型謂詞 ..................................................................................................................240 16.4.2 條件屬性 ..................................................................................................................242 16.4.3 類型生成器 ..............................................................................................................242 16.4.4 關(guān)聯(lián)類型 ..................................................................................................................243 16.5 source_location ........................................................................................................ 244 16.6 move()和 forward().................................................................................................. 244 16.7 位操作 ..................................................................................................................... 246 16.8 退出程序 ................................................................................................................. 247 16.9 建議 ......................................................................................................................... 248 第 17 章 數(shù)值計(jì)算...............................................................................................................249 17.1 引言 ......................................................................................................................... 249 17.2 數(shù)學(xué)函數(shù) ................................................................................................................. 250 17.3 數(shù)值計(jì)算算法 ......................................................................................................... 251 17.3.1 并行數(shù)值算法 ..........................................................................................................251 17.4 復(fù)數(shù) ......................................................................................................................... 252 17.5 隨機(jī)數(shù) ..................................................................................................................... 253 17.6 向量算術(shù) ................................................................................................................. 255 17.7 數(shù)值界限 ................................................................................................................. 256 17.8 類型別名 ................................................................................................................. 256 17.9 數(shù)學(xué)常數(shù) ................................................................................................................. 256 17.10 建議 ....................................................................................................................... 257 第 18 章 并發(fā)......................................................................................................................259 18.1 引言 ......................................................................................................................... 259 18.2 任務(wù)和 thread .......................................................................................................... 260 18.2.1 傳遞參數(shù) ..................................................................................................................262 18.2.2 返回結(jié)果 ..................................................................................................................262 18.3 共享數(shù)據(jù) ................................................................................................................. 263 18.3.1 mutex 和鎖 ...............................................................................................................263 18.3.2 原子量......................................................................................................................265 18.4 等待事件 ................................................................................................................. 266 18.5 任務(wù)間通信 ............................................................................................................. 268 18.5.1 future 和 promise ......................................................................................................268 18.5.2 packaged_task...........................................................................................................269 18.5.3 async() ......................................................................................................................270 18.5.4 停止 thread ...............................................................................................................271 18.6 協(xié)程 ......................................................................................................................... 273 18.6.1 協(xié)作式多任務(wù) ..........................................................................................................274 18.7 建議 ......................................................................................................................... 277 第 19 章 歷史和兼容性........................................................................................................279 19.1 歷史 ......................................................................................................................... 279 19.1.1 大事年表 ..................................................................................................................280 19.1.2 早期的 C++ ..............................................................................................................281 19.1.3 ISO C++標(biāo)準(zhǔn)............................................................................................................284 19.1.4 標(biāo)準(zhǔn)與編程風(fēng)格 ......................................................................................................286 19.1.5 C++的使用...............................................................................................................287 19.1.6 C++模型...................................................................................................................287 19.2 C++特性演化 .......................................................................................................... 288 19.2.1 C++11 語言特性 ......................................................................................................288 19.2.2 C++14 語言特性 ......................................................................................................289 19.2.3 C++17 語言特性 ......................................................................................................290 19.2.4 C++20 語言特性 ......................................................................................................290 19.2.5 C++11 標(biāo)準(zhǔn)庫組件 ..................................................................................................291 19.2.6 C++14 標(biāo)準(zhǔn)庫組件 ..................................................................................................292 19.2.7 C++17 標(biāo)準(zhǔn)庫組件 ..................................................................................................292 19.2.8 C++20 標(biāo)準(zhǔn)庫組件 ..................................................................................................292 19.2.9 移除或棄用的特性 ..................................................................................................293 19.3 C/C++兼容性 .......................................................................................................... 294 19.3.1 C 與 C++是兄弟.......................................................................................................294 19.3.2 兼容性問題 ..............................................................................................................295 19.4 參考文獻(xiàn) ................................................................................................................. 298 19.5 建議 ......................................................................................................................... 301 附錄 A std 模塊...................................................................................................................303
你還可能感興趣
我要評(píng)論
|