Interview Kickstart has enabled over 21000 engineers to uplevel.
The Standard Template Library is a set of C++ template classes and contains incredibly useful container classes, algorithms, methods, and iterators. One such container in C++ STL is a vector. This article will help you learn more about it so you can explore how to harness its power as a tool. We’ll discuss:
Arrays work well for handling sequences but have this one major limitation: they have a fixed size once declared. The container Vector offers a solution to this problem.
A vector is a sequence container that represents a dynamic array. So it holds sequences in contiguous memory locations just like an array. But its size can also change dynamically, as the container handles the storage automatically.
We can use a vector using the following syntax:
#include<vector>
std::vector<dataType> vectorVariableName;
With vectors, we get the benefits of an array, like accessing elements easily at any position using indices, while not being limited by the static size limitation. The same thing (storage in contiguous locations) that allows arrays and vectors quick access to any element in the array also slows down insertion and deletion operations from the middle.
Shifting the rest of the elements is necessary after removing or inserting an element from the middle, which causes the slowdown.
Hence, vectors share this limitation of having slow insertion and deletion operations from the middle with arrays. Like for other containers in STL, we can iterate over elements in a Vector using an iterator. We can do several other complex operations easily since they are available as member functions for vectors. Let us take a closer look at them.
Member functions provided for a vector in CPP help us harness the power of a vector as a container. We can utilize these member functions easily and efficiently. Vectors have some basic public member functions:
Other than that, there are five types of public member functions associated with vectors in C++:
Now we’ll see how we can use these member functions through an example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorVariableName={1,2,3,4,5};
cout << "Element access member functions\n";
cout<< "\nvalue at index 2: "<< vectorVariableName[2];
cout<< "\nvalue at index 2: "<< vectorVariableName.at(2);
cout<< "\nfirst element: "<< vectorVariableName.front();
cout<< "\nlast element: "<< vectorVariableName.back();
cout<< "\naddress of vector or address of first element: "<< vectorVariableName.data();
cout << "\n"<< "\nCapacity member functions\n";
cout<<"\nvector size: "<< vectorVariableName.size();
cout<<"\nvector max size: "<< vectorVariableName.max_size();
cout<<"\nvector capacity: "<< vectorVariableName.capacity();
cout<<"\nIs vector empty: "<< vectorVariableName.empty();
vectorVariableName.resize(10,100);
cout<<"\nvector size after resizing: "<< vectorVariableName.size();
vectorVariableName.reserve(50);
cout<<"\nvector capacity after reserving more memory: "<< vectorVariableName.capacity();
vectorVariableName.shrink_to_fit();
cout<<"\nvector capacity after shrinking to fit: "<< vectorVariableName.capacity();
cout << "\n"<<"\nIterator member functions\n";
cout << "\nUsing begin and end: ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
cout << "\nUsing cbegin and cend: ";
for (auto i = vectorVariableName.cbegin(); i != vectorVariableName.cend(); ++i)
cout << *i << " ";
cout << "\nUsing rbegin and rend: ";
for (auto j = vectorVariableName.rbegin(); j != vectorVariableName.rend(); ++j)
cout << *j << " ";
cout << "\nUsing crbegin and crend: ";
for (auto j = vectorVariableName.crbegin(); j != vectorVariableName.crend(); ++j)
cout << *j << " ";
cout << "\n"<< "\nModifier member functions\n\n";
cout <<"Vector before push_back(22): ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
cout <<"\n";
//adding 22 to the vector
vectorVariableName.push_back(22);
cout <<"Vector after push_back(22): ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
cout <<"\n";
//assign 5 values, all equal to 1
vectorVariableName.assign(5,1);
cout <<"Vector after assign(5,1): ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
cout <<"\n";
//inserting value 1000 at index 0
vectorVariableName.insert(vectorVariableName.begin(), 1000);
cout <<"Vector after insert 1000 at beginning: ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
cout <<"\n";
//removing one value from the end
vectorVariableName.pop_back();
//erasing value at index 2
vectorVariableName.erase(vectorVariableName.begin()+2);
cout <<"Vector after pop_back and erase begin()+2: ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
cout <<"\n";
//creating and inserting value 101 at the end of the vector
vectorVariableName.emplace_back(101);
//creating and inserting value 0 at index 5
vectorVariableName.emplace(vectorVariableName.begin()+5, 3);
//Checking the vector now
cout << "\nVector after emplace operations: ";
for (auto i = vectorVariableName.begin(); i != vectorVariableName.end(); ++i)
cout << *i << " ";
//creating new vector, swapping values with original
vector<int> anotherVector;
vectorVariableName.swap(anotherVector);
cout << "\nNew vector after swapping contains: ";
for (auto i = anotherVector.begin(); i != anotherVector.end(); ++i)
cout << *i << " ";
//removing all elements from new vector
anotherVector.clear();
cout << "\nThe size of the new vector after using clear: "<< anotherVector.size();
cout << "\n"<< "\nAllocator member function\n";
anotherVector.get_allocator().allocate(10);
cout << "\nThe capacity of the vector after using allocate(10): "<<anotherVector.capacity();
return 0;
}
Element access member functions
value at index 2: 3
value at index 2: 3
first element: 1
last element: 5
address of vector or address of first element: 0x9eb7a10
Capacity member functions
vector size: 5
vector max size: 1073741823
vector capacity: 5
Is vector empty: 0
vector size after resizing: 10
vector capacity after reserving more memory: 50
vector capacity after shrinking to fit: 10
Iterator member functions
Using begin and end: 1 2 3 4 5 100 100 100 100 100
Using cbegin and cend: 1 2 3 4 5 100 100 100 100 100
Using rbegin and rend: 100 100 100 100 100 5 4 3 2 1
Using crbegin and crend: 100 100 100 100 100 5 4 3 2 1
Modifier member functions
Vector before push_back(22): 1 2 3 4 5 100 100 100 100 100
Vector after push_back(22): 1 2 3 4 5 100 100 100 100 100 22
Vector after assign(5,1): 1 1 1 1 1
Vector after insert 1000 at beginning: 1000 1 1 1 1 1
Vector after pop_back and erase begin()+2: 1000 1 1 1
Vector after emplace operations: 1000 1 1 1 101 3
New vector after swapping contains: 1000 1 1 1 101 3
The size of the new vector after using clear: 0
Allocator member function
The capacity of the vector after using allocate(10): 20
Question 1: What are some differences between arrays and vectors?
Question 2: How is storage handled dynamically in a vector?
Vectors internally use a dynamically allocated array for storage. Since allocating a larger array for each insertion is expensive, vectors don’t heavily rely on that. That said, they do use this when necessary. Instead, vectors usually allocate some extra space to accommodate the possibility of insertion of more elements.
Hence the vector’s actual capacity is often more than what’s required, making it less memory efficient than arrays. When we need to insert a new element and the vector is full, the vector increases its size to twice the current size.
Question 3: What are some differences between lists and vectors?
Are you a software engineer looking for coding interview questions to aid your technical interview prep? Check out these useful pages for software developers:
Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
————
Article contributed by Tanya Shrivastava
Attend our webinar on
"How to nail your next tech interview" and learn