前言:
俗话说的好,高级程序员就应该用高级的容器,可以减少常见简单代码的重复,和忘记考虑特判造成的边界错误。
几大特性:
1.顺序序列
顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。
2.动态数组
支持对序列中的任意元素进行快速直接访问,甚至可以通过指针算述进行该操作。操供了在序列末尾相对快速地添加/删除元素的操作。
3.能够感知内存分配器的(Allocator-aware)
容器使用一个内存分配器对象来动态地处理它的存储需求。
Vector
Vectors are sequence containers representing arrays that can change in size.
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.
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.
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).
Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.
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.
常用操作函数:
Iterators(泛型指针):
begin
Return iterator to beginning (public member function )
返回一个迭代器,它指向容器的第一个元素。end
Return iterator to end (public member function )
返回一个迭代器,它指向容器c的最后一个元素的下一个位置。
rbegin
Return reverse iterator to reverse beginning (public member function )
返回一个逆序迭代器,它指向容器c的最后一个元素rend
Return reverse iterator to reverse end (public member function )
返回一个逆序迭代器,它指向容器c的第一个元素前面的位置
Return const_iterator to beginning (public member function )
Return const_iterator to end (public member function )
Return const_reverse_iterator to reverse beginning (public member function )
Return const_reverse_iterator to reverse end (public member function )
功能与上两组相似,但是返回的是常量迭代器,不能进行修改操作。
Capacity(储存):
size
Return size (public member function )
返回元素个数
max_size
Return maximum size (public member function )
返回最大的容量resize
Change size (public member function )
更改容量大小capacity
Return size of allocated storage capacity (public member function )
返回已分配存储容量的大小
empty
Test whether vector is empty (public member function )
判断是否为空
reserve
Request a change in capacity (public member function )
重新分配存储空间,注意不要跟reverse搞混。shrink_to_fit
Shrink to fit (public member function )
自动优化容器大小。
Element access(元素的访问):
operator[]
Access element (public member function )
可以跟数组一样通过下标访问元素。
at
Access element (public member function )
你还可以通过at(i)来轻松访问^_^!front
Access first element (public member function )
返回第一个数值back
Access last element (public member function )
返回最后一个数值
data
Access data (public member function )
返回一个指向数据头部的指针,类型与你定义的相同(vector是连续存储的,可以通过指针的移动来访问元素)
modifiers(操作):
assign
Assign vector content (public member function )
区间或者指定位置赋值,有3种操作方式。push_back
Add element at the end (public member function )
将一个元素放到最后pop_back
Delete last element (public member function )
将最后一个元素删除insert
Insert elements (public member function )
插入,支持多种插入方式。erase
Erase elements (public member function )
清除,支持多种清楚方式swap
Swap content (public member function )
交换两个容器clear
Clear content (public member function )
清空一个容器emplace
Construct and insert element (public member function )
插入一个数值,emplace() 在插入元素时,是在容器的指定位置直接构造元素,而不是先单独生成,再将其复制(或移动)到容器中,所以速度相比于insert会快一些。但是如果在中间,仍要花费O(N)来移动插入位置后的N个元素。
emplace_back
Construct and insert element at the end (public member function )
在尾部插入数值。
Allocator(分配算符):
get_allocator
Get allocator (public member function )
返回vector的内存分配器,一般用不到。