Vectors are sequence container (same as dynamic arrays) which resizes itself automatically. The size changes (i.e. vector can shrink or expand as needed at run time) when an element is inserted or deleted, with their storage being handled automatically by the container.

Vector elements are placed in contiguous memory locations (just like arrays) so that they can be accessed and traversed using iterators i.e. subscript operator []. Vector elements can also be accessed by using offsets to regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.

In vectors, data is inserted at the end and inserting data at the end may require extending the array. To support shrink and expand functionality at runtime, vector container may allocate some extra storage to accommodate for possible growth thus container have actual capacity greater than the size.

Therefore, 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. However, vector consumes more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Vector Definition

Below is definition of std::vector from <vector> header library file

Vector Parameters

  • T – The type of elements. T may be substituted by any other data type including user-defined type.
  • Allocator − Type of allocator object.By default, the allocator class template is used, which defines the memory allocation/de-allocation model, construct/destroy elements and is value-independent.

Vector Functions

Vectors library provides lots of functions to traverse, access and manipulate vectors. There are a lot many helper/utility vector functions to determine the capacity and size of vectors.

Traversing Vectors – Iterators

There are four basic functions associated with vectors which are used to traverse vectors i.e.

  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 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 preceding the first element in the vector (considered as reverse end)

C++ Program to demonstrate Vector Iterators

Output:

Accessing Vector Elements  – Access Functions

Vector elements can be accessed using the following vector functions. Any/combination or all of these functions can be used to access vector elements in different situations.

  1. front()  Returns a reference to the first element in the container.
  2. back()  Returns reference to the last element in the container.
  3. at()  Returns a reference to the element at specific position in the vector by using a reference operator (as mentioned below)
  4. Reference Operator [r]  Returns a reference to the element at position ‘r’ in the vector

C++ Program to Access Vector Elements

Output:

Manipulate Vector Elements  – Modifier Functions

  1. insert()  Inserts elements into the vector.
  2. assign()  – Assigns new content to vector and resize
  3. emplace()  – Constructs element in-place
  4. push_back()  – Adds an element to the end of the vector and increment the size
  5. emplace_back() – Constructs an element in-place at the end
  6. pop_back() – Removes the last element of the vector and decrement the size
  7. resize() – Changes the number of elements stored
  8. swap() – Swaps the contents of two vectors
  9. clear() – Clears the contents of the vector
  10. erase() – Removes the specific element or range of elements from vector

C++ Program to Manipulate Vector Elements

Output:

Capacity and Size Functions

  1. empty()  – This function checks whether the container is empty or not.
  2. size() – Returns the number of elements in the vector
  3. max_size() – Returns the maximum possible number of elements of vector
  4. reserve() – Reserves storage i.e. increase the capacity of the vector to a value that’s greater or equal to new_cap.
  5. capacity() – Returns the number of elements that can be held in currently allocated storage
  6. shrink_to_fit() – Reduces memory usage by freeing unused memory

C++ Program to display number of elements in Vector

Output:

Zero Sized Vectors

Zero sized vectors are also possible and valid. In that case vector.begin() and vector.end() points to same location. But behavior of calling front() or back() is undefined.

 

Please write comments if you find anything incorrect, or you want to provide feedback or share more information about this topic.

3 Shares