C++核心准则​​SL.con.2:除非有理由使用其他容器,默认使用STL vector

2020-10-30 11:29:30 浏览数 (1)

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具有下面的优势:

  • 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。
Note(注意)

To initialize a vector with a number of elements, use ()-initialization. To initialize a vector with a list of elements, use {}-initialization.

如果希望用元素数量初始化vector,使用()初始化形式。如果希望使用元素列表初始化数组,使用{}形式。

代码语言:javascript复制
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.

ES.23:优先使用{}初始化器语法

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。

原文链接

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

0 人点赞