Vectors in C++: A beginner’s guide
By: Aman Kumar Goyal
Tags: Competitive Programming, Computer Science, Programming, Vector, Cpp

Hey folks, In this blog I am going to introduce you to vectors and its basic functionalities and capabilities of a vector.

You might be working with arrays and vectors are no different than that but they comes with additional features, capabilities and many powerful inbuilt functions and methods.

Vectors are dynamic arrays which can change their size dynamically at runtime. Continuous space is allocated which makes it easier to iterate over the vector and it takes linear time to insert or remove element at position other than that of end point, it takes constant time to insert or remove element from the last position. Generally elements are inserted at the end.

Compared to the other dynamic sequence containers, vectors are very efficient accessing its elements and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others.

Getting Started

To use vectors you have to first include the vector library:

#include<vector>

You can define a vector of any type you want whether it be an integer vector or float type, char type, a vector of strings, or it can also be a vector of pairs or a vector of vectors

vector<int> v; //vector of integers

vector<float> v; //vector of floating point numbers

vector<char> v; //vector of characters

vector<string> v; //vector of string

vector< pair<int, int> > v; //vector of pairs of integers

vector< vector<int> > v; //vector of vectors

You can even specify the size of your vector beforehand.

vector<int > v(n);

To insert elements in vector we generally use the inbuilt push_back method.

for(i=0; i<n; i++){

cin>>x;

v.push_back(x);

}

You can iterate over the vector just like array which makes it really easier to work with, also you can refer to a particular element same as in array using indexes.

for(i=0 ; i<n ; i++){

cout>>a[i]; // indexing same as array

}

You can also use the inbuilt sort function with vectors.

sort(v.begin(), v.end());

Now, we have seen that vectors are just a little bit different than array when it comes to implementation from here we will see all the inbuilt methods and functions which makes vector a much powerful data structure as compared to simple array.

Iterators

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

→Returns an iterator pointing to the first element in the vector. end() →Returns an iterator pointing to the theoretical element that follows the last element in the vector.

→Returns an iterator pointing to the theoretical element that follows the last element in the vector. rbegin() (reverse beginning) →Returns a reverse iterator pointing to the last element in the vector. It moves from last to first element.

(reverse beginning) →Returns a reverse iterator pointing to the last element in the vector. It moves from last to first element. rend() ( reverse end)→Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector.

( reverse end)→Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector. cbegin() →Returns a constant iterator pointing to the first element in the vector.

→Returns a constant iterator pointing to the first element in the vector. cend() →Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.

→Returns a constant iterator pointing to the theoretical element that follows the last element in the vector. crbegin() →Returns a constant reverse iterator pointing to the last element in the vector . It moves from last to first element.

→Returns a constant reverse iterator pointing to the last element in the vector . It moves from last to first element. crend() →Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector.

Capacity

size() → Returns the number of elements in the vector.

→ Returns the number of elements in the vector. max_size() → Returns the maximum number of elements that the vector can hold.

→ Returns the maximum number of elements that the vector can hold. capacity() → Returns the size of the storage space currently allocated to the vector expressed as number of elements.

→ Returns the size of the storage space currently allocated to the vector expressed as number of elements. resize(n) → Resizes the container so that it contains ’n’ elements.

→ Resizes the container so that it contains ’n’ elements. empty() → Returns whether the container is empty.

→ Returns whether the container is empty. shrink_to_fit() → Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.

→ Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity. reserve() → Requests that the vector capacity be at least enough to contain n elements.

Modifiers

assign() → It assigns new value to the vector elements by replacing old ones.

→ It assigns new value to the vector elements by replacing old ones. push_back() → It push the elements into a vector from the back.

→ It push the elements into a vector from the back. pop_back() → It is used to pop or remove elements from a vector from the back.

→ It is used to pop or remove elements from a vector from the back. insert() → It inserts new elements before the element at the specified position.

→ It inserts new elements before the element at the specified position. erase() → It is used to remove elements from a container from the specified position or range.

→ It is used to remove elements from a container from the specified position or range. swap() → It is used to swap the contents of one vector with another vector of same type. Sizes may differ.

→ It is used to swap the contents of one vector with another vector of same type. Sizes may differ. clear() → It is used to remove all the elements of the vector container.

→ It is used to remove all the elements of the vector container. emplace() → It extends the container by inserting new element at position.

→ It extends the container by inserting new element at position. emplace_back() → It is used to insert a new element into the vector container, the new element is added to the end of the vector.

Element Access

reference operator [g] → Returns a reference to the element at position ‘g’ in the vector.

→ Returns a reference to the element at position ‘g’ in the vector. at(g) → Returns a reference to the element at position ‘g’ in the vector.

→ Returns a reference to the element at position ‘g’ in the vector. front() → Returns a reference to the first element in the vector.

→ Returns a reference to the first element in the vector. back() → Returns a reference to the last element in the vector.

→ Returns a reference to the last element in the vector. data() → Returns a direct pointer to the memory array used internally by the vector to store its owned elements.

Here comes the end….

That’s it here comes the end of this blog and I hope you like it and you have learned some basic concepts of vector, now go and play with the concepts you have learned try to get your hands dirty by writing some code and you can read more about vector through websites like cppreference.com there are many resources out there.

Keep Learning and Happy Programming :-)