Vectors are sequential containers that are very similar to arrays. They are continuous blocks of memory. Unlike arrays, vectors can have dynamic sizes. Vectors are one of the most useful containers in STL. Vector is present in #include<vector>header file. The elements inside the vector can be accessed using iterators.
Vector Declaration
Syntax:
vector<data_type> name(size,initial_value);
size: optional parameter which tells us the size of the vector
initial_value: optional parameter which initializes the vector with the given value
Example:
vector < int > v; //initializes a vector of size 0 which stores integer values
vector < int > v(10); //initializes a vector of size 10 having initial value as 0
vector < int > v(10, 1); //initializes a vector of size 10 having initial value as 1Functions on vectors
push_back(element): It pushes the value at the end of the vector.
Time Complexity: O(1)
Parameters: the element which is to be inserted
Return type: void
size(): It tells us the size of the vector.
Time Complexity: O(1)
Parameters: None
Return type: integer - total number of elements in the vector
pop_back(): It deletes the last element of the vector.
Time Complexity: O(1)
Parameters: None
Return type: void
clear(): It deletes all the elements from the vector.
Time Complexity: O(N) where N is the size of the vector
Parameters: None
Return type: void
empty(): It tells us whether the vector is empty or not.
Time Complexity: O(1)
Parameters: None
Return type: Boolean, true if vector is empty else false
#include<iostream>
#include<vector>
using namespace std;
void print(vector < int > v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
return;
}
int main() {
vector < int > v;
for (int i = 0; i < 5; i++) {
v.push_back(i + 1);
}
cout << "Size of vector is " << v.size() << '\n';
print(v);
v.pop_back();
print(v);
v.clear();
if (v.empty() == true) {
cout << "vector is empty!";
}
return 0;
}Output
Size of vector is 5
1 2 3 4 5
1 2 3 4
vector is empty!Copying one vector to another
vector < int > v2 = v1; //copies vector1 into vector2 Any change in v2 will not affect v1. The time complexity of this operation is O(N) where N is the size of the vector1. The same can be done using reference but it will not create a copy, it will contain a reference to the original vector.
vector < int > & v2 = v1; //v2 references v1Passing vector by value to a function
#include<iostream>
#include<vector>
using namespace std;
void print(vector < int > v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return;
}
int main() {
int n;
cin >> n;
vector < int > v(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
print(v);
return 0;
}When the vector v is passed to the print function from the main function, a copy is created which takes O(N) time.
Passing vector by reference to a function
#include<iostream>
#include<vector>
using namespace std;
void print(vector < int > & v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return;
}
int main() {
int n;
cin >> n;
vector < int > v(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
print(v);
return 0;
}When the vector v is passed to the print function from the main function, the reference to vector v is passed. This prevents us from creating a copy of the vector.
Nesting in vectors
Array of vectors
vector < int > arr[10]; //an array arr whose each element is a vectorAn array of vectors behaves like a 2D array but it has variable size columns.
vector of vectors
vector < vector < int >> v; //a vector v whose each element is a vectorvector of vectors behaves like a 2D array but it has variable size rows and columns.