5.19 auto
vector<string> v;auto s1=v[0]; // 創(chuàng)建一份v[0]const auto& s2=v[0];優(yōu)點(diǎn): C++類型名有時(shí)很長(zhǎng)的, spare_hash_map<string,int>::iterator iter= m.find(val); 返回類型是不是很難讀啊 不過(guò)代碼是可以重構(gòu)成 auto iter = m.find(val);
auto 只能用在局部變量里,其他地方別用。 auto 和C++11 特性[尾置返回類型 trailing return type] 一起用, 不過(guò)后者只能用在lambda表達(dá)式里。
5.20 列表初始化
5.21. Lambda 表達(dá)式 適當(dāng)?shù)氖褂茫@示使用Lambda捕獲,不要使用默認(rèn)的。 Lambda表達(dá)式是創(chuàng)建匿名函數(shù)對(duì)象的一種簡(jiǎn)易途徑,常用于把函數(shù)當(dāng) 做參數(shù)傳遞。
std::sort(v.begin(),v.end(),[](int x,int y){ return Weight(x)<Weight(y);});(polymorhpic wrapper) std::function
優(yōu)點(diǎn): 傳函數(shù)對(duì)象給STL算法,Lambdas 非常方便
缺點(diǎn): Lambdas 的變量捕獲旁門左道,可能造成懸空指針 層層的嵌套的Lambda的匿名函數(shù)難以閱讀。
顯示捕獲 [n](int x) {return x+n;} 這樣讀者更加容易知道n是捕獲值5.22 模板編程 不要使用復(fù)雜的模板編程。
利用C++模板實(shí)例化機(jī)制是圖靈完備性??梢杂脕?lái)實(shí)現(xiàn)編譯時(shí)刻 的類型判斷的一系列的技巧。
優(yōu)點(diǎn): 模板編程能夠?qū)崿F(xiàn)非常靈活的類型安全的接口和極好的性能。
5.23 Boost 庫(kù) 只使用Boost庫(kù)中被認(rèn)可的庫(kù)
什么是Boost庫(kù)? Boost庫(kù)是一個(gè)廣受歡迎的,經(jīng)過(guò)同行鑒定的免費(fèi)開(kāi)源的C++庫(kù)
6.命名約定
6.1. 通用命名規(guī)則
6.3. 類型命名 所有類型命名 —— 類, 結(jié)構(gòu)體, 類型定義 類型名稱的每個(gè)單詞首字母均大寫, 不包含下劃線
全局變量:可以用 g_ 或其它標(biāo)志作為前綴, 以便更好的區(qū)分局部變量.
6.5. 常量命名 const int kDaysInAWeek = 7; 6.6. 函數(shù)命名 … 7. 注釋 7.2. 文件注釋 在每一個(gè)文件開(kāi)頭加入版權(quán)公告, 然后是文件內(nèi)容描述. 1.版權(quán)聲明 (比如, Copyright 2008 Google Inc.) 2.許可證. 為項(xiàng)目選擇合適的許可證版本 (比如, Apache 2.0, BSD, LGPL, GPL) 3.作者: 標(biāo)識(shí)文件的原始作者.
7.3. 類注釋
每個(gè)類的定義都要附帶一份注釋, 描述類的功能和用法.
// Iterates over the contens of a GargantuanTable. sample usage// GargantuanTable_Iteratar * iter = table->NewIterator()// for(iter->Seek("foo");!iter->done();iter->Next()){// PRocess(iter->key(),iter->value());//}// delete iter;class GargantuanTable_Iteratar{ ...};7.4. 函數(shù)注釋
函數(shù)的輸入輸出. 對(duì)類成員函數(shù)而言: 函數(shù)調(diào)用期間對(duì)象是否需要保持引用參數(shù), 是否會(huì)釋放這些參數(shù). 如果函數(shù)分配了空間, 需要由調(diào)用者釋放. 參數(shù)是否可以為 NULL. 是否存在函數(shù)使用上的性能隱患. 如果函數(shù)是可重入的, 其同步前提是什么?
// Returns an iterator for this table. It is the client's// responsibility to delete the iterator when it is done with it,// and it must not use the iterator once the GargantuanTable object// on which the iterator was created has been deleted.//// The iterator is initially positioned at the beginning of the table.//// This method is equivalent to:// Iterator* iter = table->NewIterator();// iter->Seek("");// return iter;// If you are going to immediately seek to another place in the// returned iterator, it will be faster to use NewIterator()// and avoid the extra seek.Iterator* GetIterator() const;注明構(gòu)造函數(shù)對(duì)參數(shù)做了什么 (例如, 是否取得指針?biāo)袡?quán)) 以及析構(gòu)函數(shù)清理了什么.
7.5. 變量注釋
7.6 實(shí)現(xiàn)注釋
注意 永遠(yuǎn)不要 用自然語(yǔ)言翻譯代碼作為注釋. 要假設(shè)讀代碼的人 C++ 水平比你高, 即便他/她可能不知道你的用意:
7.7. 標(biāo)點(diǎn), 拼寫和語(yǔ)法
7.8. TODO 注釋 對(duì)那些臨時(shí)的, 短期的解決方案, 或已經(jīng)夠好但仍不完美的代碼使用 TODO 注釋
7.9. 棄用注釋 通過(guò)棄用注釋(DEPRECATED comments)以標(biāo)記某接口點(diǎn)(interface points)已棄用
8.格式 8.4. 函數(shù)聲明與定義
8.5. Lambda 表達(dá)式
其它函數(shù)怎么格式化形參和函數(shù)體,Lambda 表達(dá)式就怎么格式化;捕獲列表同理。
若用引用捕獲,在變量名和 & 之間不留空格。 8.10. 指針和引用表達(dá)式 句點(diǎn)或箭頭前后不要有空格. 指針/地址操作符 (*, &) 之后不能有空格.
8.13. 變量及數(shù)組初始化
用 =, () 和 {} 均可.
8.15. 類格式 訪問(wèn)控制塊的聲明依次序是 public:, protected:, private:, 每次縮進(jìn) 1 個(gè)空格.
8.17. 名字空間格式化 名字空間 不要增加額外的縮進(jìn)層次
8.18. 水平留白
9.規(guī)則特例
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注