C++ Standard Template Library (STL) provides the data structure, Vector, that represents a dynamic array internally. As the vector is a template class, it can represent an array of virtually any data type. Here we’ll see how the Vector can represent an integer array.
The vector data structure has many advantages over a traditional array.
We don’t need to worry about the size at all. In case of an array, we should know the maximum number of elements that the array can hold beforehand. Then we need to allocate space for that many elements – statically or dynamically. No matter how many elements the array will hold for a particular moment. If the array holds much less number of elements compared to the actual size, then a lot of memory will unnecessary be wasted.
The bigger problem is if we want to add more elements than the originaly size. If the array size is statically allocated, we’ll not be able to use the same array to accommodate the new elements. We’ll have to allocate a bigger array and then copy the existing elements from the old one and put the new elements. If the array is dynamically allocated, then we’ll have to reallocate / resize the array explicitly.
Vector handles all these things internally and tries to allocate optimum size based on the number of elements at a particular moment.
Inserting an element in the middle of an array is a pain! Same thing is true for deleting a middle element. We’ll need to manually relocate many elements. Vector does these things under the hood.
The individual vector elements are access just like accessing an array element.
The Problem Statement
We’ll see how to use Vector for an integer array by solving this problem.
- Take the vector size and the elements as input.
- Print the inserted elements.
- Insert a new element and insert that into a specified position. Print the vector elements after insertion.
- Delete an element from a specified position. Print the array after deletion.
- Take a position and print the vector element of that position.
The Vector Program
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vect;
int n, pos, tmp;
cout << "Enter the number of elements: ";
cin >> n;
cout << "Enter " << n << " integers:" << endl;
for (auto i = 0; i < n; i++) {
cin >> tmp;
vect.push_back(tmp);
}
cout << "The entered vector:" << endl;
for (auto m : vect) {
cout << m << " ";
}
cout << endl;
cout << "Insert a new element: ";
cin >> tmp;
cout << "Insert the position: ";
cin >> pos;
/*Sanity checking*/
if (pos > vect.size()) {
cout << "Invalid position. Exiting..." << endl;
return -1;
}
vect.insert(vect.begin() + pos, tmp);
cout << "The vector after inserting " << tmp << " at " << pos <<"-th position:" << endl;
for (auto m : vect) {
cout << m << " ";
}
cout << endl;
cout << "Enter a position to delete an element from: ";
cin >> pos;
/*Sanity checking*/
if (pos >= vect.size()) {
cout << "Invalid position. Exiting..." << endl;
return -1;
}
vect.erase(vect.begin() + pos);
cout << "The vector after deleting the " << pos <<"-th element:" << endl;
for (auto m : vect) {
cout << m << " ";
}
cout << endl;
cout << "Enter a position: ";
cin >> pos;
/*Sanity checking*/
if (pos >= vect.size()) {
cout << "Invalid position. Exiting..." << endl;
return -1;
}
cout << "The " << pos << "-th element is " << vect[pos] << endl;
return 0;
}
The output of this program:
$ g++ -std=c++11 -o test test.cpp
$ ./test
Enter the number of elements: 6
Enter 6 integers:
1 2 3 4 5 6
The entered vector:
1 2 3 4 5 6
Insert a new element: 10
Insert the position: 4
The vector after inserting 10 at 4-th position:
1 2 3 4 10 5 6
Enter a position to delete an element from: 4
The vector after deleting the 4-th element:
1 2 3 4 5 6
Enter a position: 4
The 4-th element is 5