T[N]
Built-in array: a fixed-size contiguously allocated sequence of N elements of type T; implicitly converts to a T*
内置数组:固定大小的连续分配的T型N个元素序列;隐式转换为T*
array<T,N>
A fixed-size contiguously allocated sequence of N elements of type T; like the built-in array, but with most problems solved
T型N个元素的固定大小连续分配序列;与内置数组类似,但大多数问题都已解决
A vector is a sequence of elements of a given type. The elements are stored contiguously in memory. A typical implementation of vector will consist of a handle holding pointers to the first element, one-past-the-last element, and one-past-the-last allocated space (§13.1) (or the equivalent information represented as a pointer plus offsets):
vector是给定类型的元素序列 。元素连续存储在存储器中 。vector的典型实现将由一个句柄组成,该句柄持有指向第一个元素的指针,一个指针位于最后一个元素之后,一个位于最后分配的空间之后(或表示为指针加偏移的等效信息):
In addition, it holds an allocator (here, alloc), from which the vector can acquire memory for its elements. The default allocator uses new and delete to acquire and release memory. Using a slightly advanced implementation technique, we can avoid storing any data for simple allocators in a vector object.
此外,它还包含一个分配器(这里是alloc),vector可以从中获取元素的内存 。默认分配器使用new和delete来获取和释放内存 。使用稍微先进的实现技术,我们可以避免在vector对象中存储简单分配器的任何数据 。
template<typename T>class Vector {allocator<T> alloc;// standard-library allocator of space for TsT* elem;// pointer to first elementT* space;// pointer to first unused (and uninitialized) slotT* last;// pointer to last slotpublic:// ...int size() const { return space-elem; }// number of elementsint capacity() const { return last-elem; }// number of slots available for elements// ...void reserve(int newsz);// increase capacity() to newsz// ...void push_back(const T& t);// copy t into Vectorvoid push_back(T&& t);// move t into Vector};
STL array, and tuple elements are contiguously allocated; list and map are linked structures.An STL array, defined in <array>, is a fixed-size sequence of elements of a given type where the number of elements is specified at compile time. Thus, an array can be allocated with its elements on the stack, in an object, or in static storage. The elements are allocated in the scope where the array is defined. An array is best understood as a built-in array with its size firmly attached, without implicit, potentially surprising conversions to pointer types, and with a few convenience functions provided. There is no overhead (time or space) involved in using an array compared to using a built-in array.
StL array和tuple元素被连续分配;list和map是链接结构 。
在<array>中定义的STL array是给定类型的元素的固定大小序列,其中元素的数量在编译时指定 。因此,可以在堆栈、对象或静态存储中为数组分配元素 。元素在定义数组的范围内分配 。STL array最好理解为一个内置数组(built-in array),其大小固定,没有隐含的、可能令人惊讶的指针类型转换,并且提供了一些方便的函数 。与使用内置数组相比,使用STL array不需要任何开销(时间或空间) 。
An array does not follow the “handle to elements” model of STL containers. Instead, an array directly contains its elements. It is nothing more or less than a safer version of a built-in array.
STL array不遵循STL容器的“句柄到元素”模型 。相反,数组直接包含其元素 。它或多或少是内置array的一个更安全的版本 。
This implies that an array can and must be initialized by an initializer list:
这意味着数组可以也必须由初始值设定项列表初始化:
array<int,3> a1 = {1,2,3};
The number of elements in the initializer must be equal to or less than the number of elements specified for the array.初始值设定项中的元素数必须等于或小于为数组指定的元素数 。
The element count is not optional, the element count must be a constant expression, the number of elements must be positive, and the element type must be explicitly stated:
元素计数不是可选的,元素计数必须是常量表达式,元素数必须为正数,并且必须明确声明元素类型:
void f(int n){array<int> a0 = {1,2,3};// error size not specifiedarray<string,n> a1 = {"John's", "Queens' "};// error: size not a constant expressionarray<string,0> a2;// error: size must be positivearray<2> a3 = {"John's", "Queens' "};// error: element type not stated// ...}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 怎么编写程序,C++程序怎么编写
- JAVA中数组排序方法都有哪些
- Docker 内置容器支持 .NET 7
- 火爆了,小白必读的优质C++开源项目
- 华擎|首创内置Wi-Fi强化天线 华擎第一款34寸电竞显示器开卖
- 超薄|轻薄机也有长续航!荣耀X40内置5100mAh大电池
- 投影仪|小米投影仪mini发布:内置电池 轻松看完一部电影
- 智能手表|3388元!华为Watch GT 3 Pro钛金属表带款发布:内置微信
- 陀螺仪|549元 万魔推出首款空间音频降噪耳机:内置陀螺仪精准定位
- 「Elasticsearch」内置数据预处理神器Ingest