Home » C++ Tutorial » What are Vectors in C++? All You Need to Know

What are Vectors in C++? All You Need to Know

As far as programming languages go, C++ is one of the most versatile of them all. That being said, C++ has a wide variety of different options and functions that the coder may choose to arrive at the desired result. One of these concepts that will ease the load on the coder is Vectors in C++. Now, before we get all technical and confuse the idea, let us start from scratch. Following is all you need to know about vectors and how they function.

Vector Definition in C++:

In the context of C++, vectors work pretty similarly to arrays. For those who do not know what an array is, it is but a collection of data. A vector is typically described to be a container object, which simply means that vectors can be used to store different types of data, such as strings, doubles, ints, etc. Vectors can be used in a number of ways, be it to store a collection of students, albums, records of employees, and so much more. But in order to use vectors, we need to import a new library which also means that we will have access to a set of predefined functions in our vector class. This will look something like this,

#include <iostream>
#include <vector>

Moving on, let’s learn how to declare a specific vector. It is a very simple process that begins with typing the keyword ‘vector’ followed by hairpins ‘< >’ that contains the data type that you wish to store within the vector, followed by the name that you wish to associate with the vector. For instance, if you wish to make a collection of letters in the alphabet, your code will look something like this:

Int main () {
// How to declare a vector
vector <char> alphabet

And there you have it, you have now declared your very first vector in C++. Now when it comes to adding data to the declared vector, all you need to do is type the name of your vector followed by a period (.) which is then followed by the command ‘push_back’ You may manually type out the command or choose from the options that will be provided to you in the dropdown box after you have added the period. After this, you will need to add in the data into brackets ‘()’ Following our above-mentioned example, the only data that we can add to our code are single characters, represented by the data type ‘char’ To define the size of the vector that you have declared, you will need to add the number of elements that your vector will contain after the name that you have associated with your vector. This will look something like this:

vector <char> alphabet(5);
alphabet.push_back(c);
// the letter ‘c’ is the data that we have added within the alphabet vector that can hold a maximum of 5 elements

Similarly, if you were to add the push_back command after your vector name again and add a value of the same data type, then that value will be added to the end of the vector. For example,

alphabet.push_back(c);
alphabet.push_back(d);
alphabet.push_back(e);
// in this way, the contents of your vector will be ‘c d e’
cout << alphabet <<

OUTPUT

c d e

To add an element or value to your vector before a specified element within the vector, we can use the insert function in C++ vectors. To do this, you need to know that adding the ‘.begin’ functions tells the system to read the elements from the beginning, or from index 0. Remember, if your vector contains 5 elements then the first element will be considered to have an index value of 0; this also implies that the 5th element will end up having the index value of 4. To specify a certain position within the vector, you will have to add on from index 0. Simply put, if you wish your new element to be placed 4th from the beginning, then you will need to add 3 (+3) to your ‘.insert’ function so that your new element or value will be placed after 3 elements from the beginning ( index 0 ). This can be demonstrated as follows:

alphabet.insert(alphabet.begin(), a);
//This will add the value ‘a’ to the beginning of the vector since no addition is mentioned
alphabet.insert(alphabet.begin() + 2, z);
//This will add the value ‘z’ after the second element in your vector, so your vector will contain the following: a b z c e

Another easy way of doing this would be to mention the index number at which you would want to insert your new element within square brackets ‘[ ]’ For instance, to add the element ‘z’ to your alphabet vector, you could use the following method:

alphabet[2] = z
// This will also result in the element ‘z’ appearing at the 2nd index position (3rd from the beginning) to show that the following will be its contents: a b z c e

This is how you insert value in a vector in C++ and change its size. Moving on to some of the other functions of vectors in C++, keeping in mind the running example in this article. 

  • Adding the function ‘.at’ followed by the desired index number in brackets will return the element at the specified index number; 
  • Adding the function ‘.size’ returns an unsigned integer equal to the number of elements in your vector.
  • Adding the function ‘.erase(vectorname.begin() + index number)’ removes the element at the specified index number.
  • Adding the function ‘.clear’ will automatically clear or delete all the elements in the declared vector.
  • Adding the function ‘.empty’ simply returns a Boolean value to represent whether the declared vector is empty or not.
  • Adding the function ‘.front’ will retrieve the value that is at the very beginning unless you specify an index number within the brackets after the function.
  • Adding the function ‘.back’ will retrieve the value that is at the very end of the vector unless you specify an index number for reference within brackets after the function.

There are many more functions one can use within vectors in C++ but the aforementioned functions are the most used ones.

Vectors are definitely a very convenient method of grouping data within C++. As you can see from this article, there are numerous ways in which you can manipulate, modify and use vectors as a tool to make C++ work in your favour. Vectors are most commonly used similar to arrays but they are fundamentally very different. Vectors are bigger than arrays in size since more room in memory is allocated to vectors for growth.

The space in memory allocated to your vector does not reallocate every time you add or remove elements from your vector. Automatic reallocation takes place when the vector is exhausted in size. It comes in handy when the size of the data to be collected is not defined, as it does not set a permanent boundary limit for any vector at the beginning. This is another reason why vectors are so widely used in programming, web development, indexing, app development, and more as it makes the whole process much, much simpler.

Leave a Comment

Pin It on Pinterest