Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.

向量与动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,并且容器自动处理其存储。 矢量元素放置在连续的存储中,以便可以使用迭代器对其进行访问和遍历。 在向量中,数据插入到最后(push_back())。 在末尾插入需要花费不同的时间,因为有时可能需要扩展阵列。 删除最后一个元素只需要固定的时间,因为不会发生大小调整。 在开始或中间插入和擦除的时间是线性的。

Certain functions associated with the vector are:

成员函数:

Iterators操作

  1. begin() – Returns an iterator pointing to the first element in the vector(返回一个指向开始的迭代器(指针))
  2. end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector(返回的是最后一个元素的迭代器(指针))
  3. rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element(反转的开始,及最后的位置)
  4. rend() – Returns a reverse iterator pointing to the theoretical element(反转的end,并不是指向第一个而是第一个之前的理论位置
    preceding the first element in the vector (considered as reverse end)
  5. cbegin() – Returns a constant iterator pointing to the first element in the vector.(返回一个常量的开始指针,无法修改)
  6. cend() – Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.(返回最后一个之后的一个理论位置的常量,无法被修改)
  7. crbegin() – Returns a constant reverse iterator pointing to the last element in
    the vector (reverse beginning). It moves from last to first element(同理常量,最后一个的位置)
  8. crend() – Returns a constant reverse iterator pointing to the theoretical
    element preceding the first element in the vector (considered as reverse end)(同理常量,第一个之前的位置)

理解:当使用了这些不同的迭代器操作可以减少操作的失误,更加准确,直观地写出更优质的代码。

Capacity容量

  1. size() – Returns the number of elements in the vector.元素的个数
  2. max_size() – Returns the maximum number of elements that the vector can hold.返回可容纳的最大地元素数量(通常超级超级大,是内存)
  3. capacity() – Returns the size of the storage space currently allocated to the vector expressed as number of elements.

    返回现在所分配的内存大小(但他是用对应的元素个数来作为返回值的)

  4. resize(n) – Resizes the container so that it contains ‘n’ elements.调整容器的大小,使其能够容纳n个元素,如果原本的容量比他大,那就不更替

  5. empty() – Returns whether the container is empty.返回vector是否为空
  6. shrink_to_fit() – Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.减小容器的容量以适应其大小,并销毁超出容量的元素
  7. reserve() – Requests that the vector capacity be at least enough to contain n elements.调整容器大小,使其最小包含n个元素
  • 需要注意的是resize会将多出的空间分配为0,而reserve不会。
  • 而shrink_to_fit仅仅适用于c++11及其以上(devc++,不支持)
  • empty()返回的是bool但是在c中1或者0也可以使用;

访问元素:

Element access:

  1. reference operator [g] – Returns a reference to the element at position ‘g’ in the vector使用类似于数组的取址符【】
  2. at(g) – Returns a reference to the element at position ‘g’ in the vector(和上面的类似,但会更好,不会越界)
  3. front() – Returns a reference to the first element in the vector最前的元素
  4. back() – Returns a reference to the last element in the vector最后的元素
  5. data() – Returns a direct pointer to the memory array used internally by the vector to store its owned elements.不接受任何参数,返回该向量的指针。

修饰符:

Modifiers:

  1. assign() – It assigns new value to the vector elements by replacing old ones重新分配(未被分配的就被删了,但是被分配的内存不变)
  2. push_back() – It push the elements into a vector from the back加一个括号内的元素到vector的最后
  3. pop_back() – It is used to pop or remove elements from a vector from the back.
  4. insert() – It inserts new elements before the element at the specified position(在指定元素之前插入一个指定的元素
    • 在指定位置loc前插入值为val的元素,返回指向这个元素的迭代器,.insert(loc,val)
    • 在指定位置loc前插入num个值为val的元素.insert(loc,num,val)
    • 在指定位置loc前插入区间[start, end)的所有元素.insert(loc,locstart,locend)
  5. erase() – It is used to remove elements from a container from the specified position or range.

    erase函数要么删作指定位置loc的元素,要么删除区间[start, end)的所有元素.返回值是指向删除的最后一个元素的下一位置的迭代器

  6. swap() – It is used to swap the contents of one vector with another vector of same type. Sizes may differ.(交换两个同类型向量的内容)

  7. clear() – It is used to remove all the elements of the vector container函数删除当前vector中的所有元素.
  8. emplace() – It extends the container by inserting new element at position用法是该函数返回一个迭代器,该迭代器指向新插入的元素。(在面向对象时emplace会更好,对于一般的数据,都可,而insert的可用性更强).emplace(loc,val)
  9. emplace_back() – It is used to insert a new element into the vector container, the new element is added to the end of the vector插入元素到最后