Vectors in C++

Vectors in C++

Vectors are dynamic arrays that can resize themselves automatically whenever an element is added or deleted. Vectors require more memory than arrays in exchange for the flexibility to manage storage and develop dynamically in an efficient manner. They can be accessed and traversed using iterators.


Some properties of vectors as a container:

  1. Elements in sequence containers are ordered in a strictly linear sequence. Like an array, elements are accessed by their position in this sequence.

  2. Allows direct access to any element in the sequence, and allows for relatively quick addition/removal of elements at the end of the sequence.

  3. The container uses an allocator object to dynamically handle its storage needs.

Member functions

  • Iterators

    1. begin(): Returns an iterator pointing to the first element in the vector.

    2. end(): Returns an iterator pointing to the end at an imaginary 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).

    4. rend(): Returns a reverse iterator pointing to the imaginary element preceding the first element in the vector (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 imaginary element that follows the last element in the vector.

    7. crbegin(): Returns a constant reverse iterator pointing to the last element in the vector.

    8. crend(): Returns a constant reverse iterator pointing to the imaginary element preceding the first element in the vector.

  • Capacity

    1. size(): Returns the total number of elements in the vector.

    2. max_size(): Returns the maximum number of elements the vector can hold.

    3. resize(n): Change the size of the vector to contain ‘n’ elements.

    4. capacity(): Returns the size of the allocated storage capacity of the vector.

    5. empty(): Returns true if the vector is empty.

    6. reserve(): Requests a change in the vector capacity to be enough to contain n elements.

    7. shrink_to_fit(): Reduces the capacity of the vector to fit its size and destroys all elements beyond the capacity.

  • Element access

    1. operator[p]: Returns a reference to the element at position ‘p’ in the vector.

    2. at(g): Returns a reference to the element at position ‘p’ in the vector. (Exceptions thrown are different.)

    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 a new value to the vector elements,

      replacing old ones

    2. push_back(): It pushes(adds) the elements into a vector from the back or end.

    3. pop_back(): It pops(removes) elements from a vector from the back or last element.

    4. insert() – It inserts new elements before the element at the specified position.

    5. erase() – Removes elements from a vector from the specified position or range.

    6. swap() – It swaps the contents of one vector with another vector of the same type. Sizes of vectors may differ.

    7. clear() – It is used to remove all the elements of the vector.

    8. emplace() – It extends the container by inserting a new element at position

    9. emplace_back() – It is used to extend and insert a new element into the vector, the new element is added to the end of the vector.

Please check code for the above functions here.


Thank you :)