Vector Basics
Introduction
Hi folks, I'm writing this one a few days after the last one as I was busy meeting MS Dhoni at an event 😉.
Coming to the point now, vectors!
Why did the vector get invited to the array's party? Because it knew how to dynamically resize the fun!
Once again, let's start with why
Let's take a simple example where you're given an array of marks of students, and you need to find the median marks, but only among people who scored >= 81. Now, a simple approach would be to:
- Filter the elements of the given array, and add the marks that are greater than or equal to 81 to a different Data Structure, let's say, toppers.
- Sort that new Data Structure.
- Get the middle element.
Now, if you observe here, we don't really know the expected size of this other data structures toppers, right? So, no doubt, using an array is still possible by having its size equal to the total number of students, as the number of toppers will definitely be less than the number of total students; but wouldn't it be better if there was a container for which specifying the size beforehand is not necessary? Then we could just keep adding the toppers to that container/data structure without any worrying about the size.
This is where vectors come into the picture. Over the next few articles, we'll not only learn about the features of vectors, we'll actually be implementing vectors using arrays & classes.
Creating a Vector
There is an in-built vector in the C++ STL (Standard Template Library). In the further part of this article, let's see how we can make use of it. Let's start with how to create a vector.
// Several ways of creating a vector in C++
#include<bits/stdc++.h>
using namespace std;
// vector<type_name> vector_name;
int main()
{
vector<int> v1; // an empty vector
vector<int> v2(5); // a vector of size 5
vector<int> v3(5, -1); // a vector of size 5 where all elements are equal to -1.
vector<int> v4 = {4, 1, 5}; // a vector of size 3, where v[0] = 4, v[1] = 1 & v[2] = 5
v1 = {2, 1, 4, 3}; // Unlike arrays, vectors can be initialized later on.
return 0;
}
Basic Vector Functions
push_back
Given a vector (empty or non-empty), this function will let us push a new element to the end of the vector.
Example 1 - Getting started!
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v;
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
v.push_back(5);
v.push_back(4);
cout << '\n';
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
return 0;
}
Output 1
5 4
Example 2 - Pushing to a vector with a non-zero initial size
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v(3); // Initialised with size 3 this time
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
v.push_back(5);
v.push_back(4);
cout << '\n';
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
return 0;
}
Output 2
0 0 0
0 0 0 5 4
Example 3 - Pushing to a vector that already has some values
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v = {4, 1, 2, 5}; // Having an initial array already
cout << "Before:\t";
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
v.push_back(5);
v.push_back(4);
cout << "\nAfter:\t";
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
return 0;
}
Output 3
Before: 4 1 2 5
After: 4 1 2 5 5 4
pop_back
As the name probably suggests, the pop_back() function gets rid of the last element in the vector.
Example 1 - Popping from a vector that already has some values
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v = {1, 3};
cout << "Before:\t";
for(int i : v)
cout << i << ' ';
v.pop_back();
cout << "\nAfter:\t";
for(int i : v)
cout << i << ' ';
return 0;
}
Output 1
Before: 1 3
After: 1
Example 2 - Popping from an empty vector
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v; // Having an initial array already
cout << "Before:\t";
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
v.pop_back();
cout << "\nAfter:\t";
for(int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
return 0;
}
Output 2
bash: line 1: 63658 Segmentation fault: 11
// It'll result in a runtime error.
More Functions
- size() : This function simply tells the current size of the vector i.e. the number of elements that are there in the vector as of now.
- resize(): You can use this function to simply alter the size of the vector to your liking.
- capacity(): In case of a vector, capacity and size are 2 different things. Capacity basically tells that as of now, how many elements can it accomodate, this changes dynamically as you might add more elements to the vector.
There are a few more functions as well. Don't worry, we'll explore all of these in the coming articles.
What's next?
In this article, we've basically covered the Why and What of vectors. Let's get to the How part in the coming articles.
As mentioned above as well, we'll implement our own custom vector class.
I remember being asked about the working of vectors in the DSA round of Bloomberg London. So, sit tight and try to wrap your brain around these concepts.