Vectors in C++

Vectors in C++

Vectors are an important data structures in C++. They work similar to arrays and have operations that makes it easy to work with them. In this article, I will brief you about

  • What are vectors
  • What are some of its important operations
  • How to work with them

What are Vectors:

Vectors are basically containers which store elements in continuous manner thus representing arrays which are dynamic in nature. Dynamic means which increases and decreases its size as elements are inserted or deleted from them. They look very efficient as they are dynamic in nature but requires more memory than arrays to support its dynamic nature.

While working with vectors, one need to include a header file:

#include<vector>

and vectors are initialized in the following manner: vector< data_type >name_of_vector

vector<int>v;

vector<int>v(n); // initializes a vector with n size

vector<int>v(n, 10); // initializes a vector with n size and each value as 10

Important Operations && their working:

At first, we will initialize our vector:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int>v;

    return 0;
}

➡ Inserting elements in the vector:

We use push_back() operation to insert elements.

  • push_back(): It is used to push/insert elements at the back of vectors, e.g. v.push_back(x), where x being data to be inserted in the vector.
    v.push_back(5);
    v.push_back(10);
    v.push_back(15);
    v.push_back(20);
    v.push_back(25);

We can also use insert() operation to insert values at specific indexes.

  • insert(): It is used to insert elements at the provided index, e.g. v.insert(v.begin() + x, data), where v.begin() gives iterator to the first element and x will be index where the element is getting inserted and data will be value to be inserted.
v.insert(v.begin(), 2);
// This will insert 2 at the 0th index or at the first position of the vector.

➡ Accessing vector elements:

To access we will use iterators begin() and end(). As they are iterators and iterators works like pointers, so we will return pointer to the location of the element.

  • begin(): It returns an iterator to the beginning of the vector, e.g. v.begin().

  • end(): Similar to begin(), it returns an iterator to the end of the vector, e.g. v.end().

There are two more operations to iterate through the vector in a reverse manner.

  • rbegin(): It returns a reverse iterator to the end or reverse beginning of the vector, e.g. v.rbegin().

  • rend(): Similar to rbegin(), it returns an iterator to the beginning or reverse end of the vector, e.g. v.rend().

vector<int>:: iterator i; //way to initialize an iterator for vector 

for(i = v.begin(); i != v.end(); i++){
    cout<<*i<<" ";  // 2 5 10 15 20 25
}

Instead of initializing an iterator, one can also use auto keyword.

//We will print the array in reverse order by using rbegin() 
//and rend() operations.

for(auto it = v.rbegin(); it != v.rend(); it++){
    cout<<*it<<" ";    // 25 20 15 10 5 2
}

If one want to access a specific element at a particular index, there are two ways to do it.

  • at/[ ]: They both are used to access elements from the vectors, e.g. v .at(x) / v[x] where x is the position of the element.
//Either use [ ] operation
cout<<v[0];   // 2 

// Or use at() operation
cout<<v.at(0)<<endl;    // 2

➡ Checking the size of vector:

  • size(): It returns the size of the vector, e.g. v.size(). It returns an integer which is the size of the array.
cout<<v.size()<<endl; // 6

If we know the size, one can also access vector elements as one does with arrays.

for(int j = 0; j<v.size(); j++){
     cout<<v[j]<<" ";   // 2 5 10 15 20 25
}

Can also check whether the vector is empty or not by using empty() operation.

  • empty(): It is used to check whether the vector is empty or not, e.g. v.empty(). It gives the output in boolean form i.e. true or false.
cout<<v.empty()<<endl; // 0

In the above code snippet, 0 represents false and 1 represents true.

➡ Deleting elements from vector:

  • pop_back(): It is used to pop/delete elements from the back of vectors, e.g. v.pop_back().
v.pop_back();

After this operation, array will look like

for(auto i = v.begin(); i != v.end(); i++){
    cout<<*i<<" ";  // 2 5 10 15 20 
}

➡ Modifying the vector:

  • erase(): It is used to erase element or a range of elements(last element is not included) from a vector, e.g. v.erase(v.begin() + 5) or v.erase(v.begin(), v.begin() + 5).

When we want to delete a single element:

v.erase(v.begin());

for(i = v.begin(); i != v.end(); i++){
    cout<<*i<<" ";    // 5 10 15 20 
}

But when we delete a range of elements:

v.erase(v.begin(), v.begin() + 2);

for(i = v.begin(); i != v.end(); i++){
    cout<<*i<<" ";  // 15 20 
}

If we want to erase the whole array at once, we can use clear() operation.

  • clear(): It is used to erase each and every element of the vector, e.g. v.clear().
v.clear();

Now if we check the vector is empty or not, it will give true as output.

cout<<v.empty();   // 1

As it represents 1 i.e. true, it concludes whole vector got erased.

Other Operations:

I have shared some important operations which are normally required when working with vectors. They have a whole bunch of other operations too. You can refer here to know more about them.


I hope now you have understood, what are vectors and how to work with them. If you have any doubts, feel free to ask.