Vector

Introduction

Vector is one of the most powerful Data Type in C++ programming language and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s Array. However, in C++, we call the vector “A Container”. As a container, a vector can contain everything such as integers, doubles, characters, bools, strings, structures and a vector can contain some vectors too. But the most important feature of vector is –“Its memory is allocated dynamically.” We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way.


Why should we learn vector?


Header File

We have to include an extra header file to use this data type. That is- #include <vector>. Under this header file, we can access all the library functions that can be used for vector data type.

Declaration and initialization of a vector

We can declare a vector in several ways. Let us see some of them at a glance and then move forward.


What operations can we make with an empty vector?


Which things should we be aware of after declaring an emtpy vector?


What happens when we use resize() function?


What if we want all of the elements of our vector become 0 (for number types) or NULL (for character types) after resize?


The last 4 questions explains how we are going to insert new elements to our vector under different circumstances. Now we will see a list of some functions along with a short description.


Important Iterator Functions

Function Work of the function
begin()
v.begin()
Returns an iterator pointing to the first element of the vector
end()
v.end()
Returns an iterator pointing to the theoretical element that follows the last element in a vector
rbegin()
v.rbegin()
Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first.
rend()
v.rend()
Returns a reverse iterator pointing to the first element in the vector (reverse end).

Let’s see their uses through code:-

    vector < int > v {1, 2, 3, 4, 5};
    
    cout << "Using begin() & end(): ";
    for(auto i = v.begin(); i != v.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;
    
    cout << "Using rbegin() & rend(): ";
    for(auto i = v.rbegin(); i != v.rend(); i++) {
        cout << *i << " ";
    }
    cout << endl;
Output:
Using begin() & end(): 1 2 3 4 5
Using rbegin() & rend(): 5 4 3 2 1

In this code, i is used as an iterator. We have used auto to keep it simple. If we do not want to use auto, we have to declare an iterator to vector like this - vector < int >::iterator i and a reverse iterator like this vector < int >::reverse_iterator j;. The we can use i in the first loop and j in the second loop.. Then the code will be like :-

    vector < int > v {1, 2, 3, 4, 5};
    vector < int >::iterator i;
    vector < int >::reverse_iterator j;
    
    cout << "Using begin() & end(): ";
    for(i = v.begin(); i != v.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;
    
    cout << "Using rbegin() & rend(): ";
    for(j = v.rbegin(); j != v.rend(); j++) {
        cout << *j << " ";
    }
    cout << endl;

Important Capacity Functions

Function Work of the function
size()
v.size()
Returns the number of elements in the vector
max_size()
v.max_size()
Returns the maximum number of elements that the vetor can hold
capacity()
v.capacity
Returns the size of the storage space currently allocated to the vector expressed as number
resize()
v.resize(n)
Resizes the container so that it can contain 'n' elements
empty()
v.empty()
Returns whether the container is empty
shrink_to_fit()
v.shrink_to_fit()
Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity
reserve()
v.reserve(n)
Requests that the vector capacity be at least enough to contain 'n' elements

Element Access Functions

Function Work of the function
reference operator [g]
v[g]
Returns a reference to the element at position 'g' in the vector
at(g)
v.at(g)
Returns a reference to the element at position 'g' in the vector
front()
v.front()
Returns a reference to the first element in the vector
back()
v.back()
Returns a reference to the last element of the vector
data()
v.data()
Returns a direct pointer to the memory array used internally by the vector to store its owned elements

Modifiers

Function Work of the function
assign()
v.assign(n, x)
It assigns new value to the vector elements by replacing old ones.
More precisely, the first n elements becomes x.
push_back()
v.push_back(x)
It pushes the elements (x) into the vector form the back
pop_back()
v.pop_back()
It removes elements from a vector from the back
insert()
v.insert(iterator position, x)
It inserts new elements before the element at specified position
erase()
v.erase(iterator position)
v.erase(iterator first, iterator last)
It removes elements from the specified position or specified range
swap()
v1.swap(v2)
It is used to swap the content of one vector with another vector of same type, size may differ
clear()
v.clear()
It is used to remove all the elements of the vector container