C++核心準則??SL.con.2:除非有理由使用其他容器,默認使用STL vector
小編:啊南 65閱讀 2020.11.17
SL.con.2:除非有理由使用其他容器,默認使用STL vector
Reason(原因)vector and array are the only standard containers that offer the following advantages:
只有vector和array具有下面的優勢:
- 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).
- 最小的空間代價(連續的內存布局沒有任何多余元素,對緩存友好)
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,或者為更有效率的在中間插入元素,當長度在數KB之內時,通常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)復雜度的搜索,容器會更大(大于數KB)而且你需要頻繁執行插入操作,從而無法導致無法維護有序vector,可以使用unordered_map或者map。
To initialize a vector with a number of elements, use ()-initialization. To initialize a vector with a list of elements, use {}-initialization.
如果希望用元素數量初始化vector,使用()初始化形式。如果希望使用元素列表初始化數組,使用{}形式。
vector<int> v1(20); // v1 has 20 elements with the value 0 (vector<int>{}) vector<int> 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. 標記構造之后長度從來沒有發生變化的vector(由于它本身為const或者沒有非const函數使用它)。改正方法:使用array代替vector。
相關推薦
- C++的cin輸入錯誤導致死循環 C++的cin輸入錯誤導致死循環簡版:int a = 0;while(true){cout "請輸入數字" endl;cina;}看似一段簡單的代碼,當胡亂輸入的時候就會導致程序死循環,無限打印“請輸入數字”。解決方法如下:int a; while(cin.fail()){cout "請輸入數字&quo…
- Java向Oracle數據庫表中插入CLOB、BLOB字段 在需要存儲較長字符串到數據庫中時往往需要使用一些特殊類型的字段,在Oracle中即blob和clob字段,一般而言:Clob字段存儲字符信息,比如較長的文字、評論,Blob字段存儲字節信息,比如圖像的base64編碼。注意,上述字段的使用均可以用其他方式替代,比如用Mon…