C++核心準則??SL.con.2:除非有理由使用其他容器,默認使用STL vector

SL.con.2: Prefer using STL?vector?by default unless you have a reason to use a different container
SL.con.2:除非有理由使用其他容器,默認使用STL vector
Reason(原因)
vector?and?array?are the only standard containers that offer the following advantages:
只有vector和array具有下面的優(yōu)勢:
the fastest general-purpose access (random access, including being vectorization-friendly);
最快的一般目的訪問(隨機訪問,包含矢量化友好)
the fastest default access pattern (begin-to-end or end-to-begin is prefetcher-friendly);
最快的默認訪問模式(從前到后,從后到前都對預抓取友好)
the lowest space overhead (contiguous layout has zero per-element overhead, which is cache-friendly).
最小的空間代價(連續(xù)的內(nèi)存布局沒有任何多余元素,對緩存友好)
Usually you need to add and remove elements from the container, so use?vector?by default; if you don't need to modify the container's size, use?array.
通常你需要對容器添加和移除元素,因此默認使用vector;如果你不需要修改容器長度,使用array。
Even when other containers seem more suited, such as?map?for O(log N) lookup performance or a?list?for efficient insertion in the middle, a?vector?will usually still perform better for containers up to a few KB in size.
即使其他容器看起來更合適,例如為了O(LogN)的搜索復雜度而考慮map,或者為更有效率的在中間插入元素,當長度在數(shù)KB之內(nèi)時,通常vector仍然可以提供更好的性能。
Note(注意)
string?should not be used as a container of individual characters. A?string?is a textual string; if you want a container of characters, use?vector*char_type*/>?or?array*char_type*/>?instead.
string不應該作為單獨字符的容器使用,string就是文本字符串;如果你需要字符容器,使用vector*char_type*/> 或者 array*char_type*/>而不是std::string。
Exceptions(例外)
If you have a good reason to use another container, use that instead.?
如果你有充分理由使用其他容器,使用它。
For example(例如):
If?vector?suits your needs but you don't need the container to be variable size, use?array?instead.
如果vector可以滿足你的需求,但是你不需要長度可變,使用array。
If you want a dictionary-style lookup container that guarantees O(K) or O(log N) lookups, the container will be larger (more than a few KB) and you perform frequent inserts so that the overhead of maintaining a sorted?vector?is infeasible, go ahead and use an?unordered_map?or?map?instead.
如果你需要字典風格的搜索容器保證O(K)或O(log N)復雜度的搜索,容器會更大(大于數(shù)KB)而且你需要頻繁執(zhí)行插入操作,從而無法導致無法維護有序vector,可以使用unordered_map或者map。
Note(注意)
To initialize a vector with a number of elements, use?()-initialization. To initialize a vector with a list of elements, use?{}-initialization.
如果希望用元素數(shù)量初始化vector,使用()初始化形式。如果希望使用元素列表初始化數(shù)組,使用{}形式。
vector v1(20); // v1 has 20 elements with the value 0 (vector{})
vector v2 {20}; // v2 has 1 element with the value 20
Prefer the {}-initializer syntax.
Enforcement(實施建議)
Flag a?vector?whose size never changes after construction (such as because it's?const?or because no non-const?functions are called on it). To fix: Use an?array?instead.
標記構(gòu)造之后長度從來沒有發(fā)生變化的vector(由于它本身為const或者沒有非const函數(shù)使用它)。改正方法:使用array代替vector。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#slcon2-prefer-using-stl-vector-by-default-unless-you-have-a-reason-to-use-a-different-container
新書介紹
《實戰(zhàn)Python設計模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。
對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎,迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學習每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
