Vector 源自CPP对vector的定义

Vectors are sequence containers representing arrays that can change in size.
Vector是能改变大小的顺序容器,用来表示数组。

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
和array一样,vector使用连续的存储空间存储元素,也就是说可以使用指向元素的常规指针加上偏移量访问元素,并且和在数组中一样高效
但和数组不一样的是,他们的大小能动态改变,其存储由容器自行处理。

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.
在内部,vector使用动态分配的数组存储元素。当插入新元素时,数组可能需要重新重新分配空间以增大数组的大小(满足增加元素的需要),这意味着要分配一个新数组并将所有的元素移动到其中。这在处理时间上是一项相对昂贵的任务,所以,vector不会每次将元素增加到容器中时重新分配(存储空间)。

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).
相反,vector容器可能分配一些额外的存储以适应可能的增长,因此容器的实际容量(capacity)可能大于严格包含其元素的存储(size)。库可以实现不同的增长策略以平衡内存使用和重新分配,但在任何情况下,重新分配都应发生在size的对数增长间隔,以使得在末端插入元素时能够平摊恒定的时间复杂度(见push_back)。

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.
因此,相比于array而言,vector消耗更多的内存以换取方便地管理存储和动态增长的能力。

Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.
相比于其他动态序列容器(deque、list、forward_list),vector就像array一样能高效地访问元素,并相对高效地增加或从尾端移除元素。对于不在尾端插入或移除元素的操作,vector比其他容器表现差,和list与forward_list相比,vector的迭代器和引用有更弱的一致性。

1 头文件
  1. #include <vector>

2 对象初始化
// 一维数组
vector<int> arr1;                 // arr1是空的数组
vector<int> arr2(n);              // arr2是一个包含n个元素的数组,每个元素被初始化为0
vector<int> arr3(n, 1);           // arr3是一个包含n个元素的数组,每个元素被初始化为1

// 用一串数初始化数组
vector<int> a({1,2,3,4});         // a.size()=4;

// 二维数组
vector<vector<int>> arr4;                                     // 0行0列的空的二维数组
vector<vector<int>> arr5(n);                                  // n行0列的空的二维数组,每行都是一个空数组
vector<vector<int>> arr6(n, vector<int>(m));                  // n行m列的二维数组,每个元素都被初始化为0
vector<vector<int>> arr7(n, vector<int>(m, 1));               // n行m列的二维数组,每个元素都被初始化为1

3 常用方法
  • 取值

    arr2[i]                     // 返回数组arr2下标i处的元素的引用
    arr2.at(i)                  // 返回数组arr2下标i处的元素的引用
    
  • 插值

    arr2.insert(itor, v)                    // 将值v插到迭代器itor指向的地方
    arr2.insert(itor, k, v)                 // 从迭代器itor指向的地方开始,连续插入k个值v
    
  • 删值

  • 其他 | insert(idx, value) | 将value插入到索引为idx的位置 | | | —- | —- | —- | | insert(idx, n, value) | 从索引为idx的位置开始,连续插入n个value | | | size() | 返回元素个数 | | | front() | 返回第一个元素的引用 | | | back() | 返回最后一个元素的引用 | | | begin() | 返回指向第一个元素的迭代器 | | | end() | 返回最后一个元素之后的迭代器 | | | erase(iterator p) | 根据迭代器指向的位置删除某元素 | | | earse(iterator first, iterator last) | 根据迭代器指向的位置删除某范围内的元素 | |