Vectors 2
Vectors 2
Dynamic Arrays
DSA&C++
Vectors
The simplest STL container is the vector, which is a data structure with contiguous
blocks of memory just like an array. Because memory locations are contiguous,
they can be randomly accessed so that the access time of any element of the
vector is constant.
To use vectors, we need to include the vector header file in our program.
#include <vector>
DSA&C++
C++ Vector declaration
Once we include the header file,then we can declare a vector in C++ as bellow
:
std::vector<T> vector_name;
The type parameter <T> specifies the type of the vector. It can be any primitive
data type such as int, char, float, etc. For example to declare a vector which
dynamically store numbers we proceed as follow :
vector<int> num;
Here, num is the name of the vector and we do not specify the size of the
vector during the declaration, because the size of a vector can grow
dynamically so it is not necessary to define it.
DSA&C++
Vector Initialisation Method 3:
DSA&C++
C++ Vector Initialization sample program
#include <iostream>
#include <vector>
using namespace std;
int main() {
// method 1
// initializer list
vector<int> vector1 = {1, 2, 3, 4, 5};
// method 2 Output
// uniform initialization
vector<int> vector2{6, 7, 8, 9, 10}; vector from method 1 = 1 2 3 4 5
// method 3
//constructor initialization vector from method 2 = 6 7 8 9 10
vector<int> vector3(5, 12);
cout << "vector from method 1 = "; vector from method 3 = 12 12 12 12 12
for (int i = 0; i < vector1.size(); i++) {
cout << vector1[i] << " ";
}
cout << "\n vector from method 2 = ";
for (int i = 0; i < vector2.size(); i++) {
cout << vector2[i] << " ";
}
cout << "\n vector from method 3 = ";
for (int i = 0; i < vector3.size(); i++) {
cout << vector3[i] << " ";
}
return 0;
}
DSA&C++
C++ Vector Initialization sample program
#include <iostream>
#include <vector>
#include <iostream> using namespace std;
#include <vector> int main() {
using namespace std; // method 1
int main() { // initializer list
// method 1 vector<int> vector1 = {1, 2, 3, 4, 5};
// initializer list // method 2
vector<int> vector1 = {1, 2, 3, 4, 5}; // uniform initialization
// method 2 vector<int> vector2{6, 7, 8, 9, 10};
// uniform initialization // method 3
vector<int> vector2{6, 7, 8, 9, 10}; //constructor initialization
// method 3 vector<int> vector3(5, 12);
//constructor initialization cout << "vector from method 1 = ";
vector<int> vector3(5, 12); for (const int& i : vector1) {
cout << "vector from method 1 = "; cout << i << " ";
for (int i : vector1) { OR }
cout << i << " "; cout << "\n vector from method 2 = ";
} for (const int& i : vector2) {
cout << "\n vector from method 2 = "; cout << i << " ";
for (int i : vector2) { } Output
cout << i << " "; cout << "\n vector from method 3 = ";
} for (const int& i : vector3) { vector from method 1 = 1 2 3 4 5
cout << "\n vector from method 3 = "; cout << i << " ";
for (int i : vector3) { }
cout << i << " "; vector from method 2 = 6 7 8 9 10
return 0;
}
return 0; vector from method 3 = 12 12 12 12
}
12
}
DSA&C++
Functions:Iterators with Vectors
DSA&C++
Iterators in Vectors
// C++ program to illustrate the
// iterators in vector
#include <iostream>
#include <vector>
return 0;
}
DSA&C++
Functions:Vector capacity
1. size() – Returns the number of elements in the vector.
2. max_size() – Returns the maximum number of elements that the vector can
hold.
3. capacity() – Returns the size of the storage space currently allocated to the
vector expressed as the number of elements.
4. resize(n) – Resizes the container so that it contains ‘n’ elements.
5. empty() – Returns whether the container is empty.
6. shrink_to_fit() – Reduces the capacity of the container to fit its size and
destroys all elements beyond the capacity.
7. reserve() – Requests that the vector capacity be at least enough to contain n
elements.
DSA&C++
Sample Program with Capacity Functions
// C++ program to illustrate the
// capacity function in vector
#include <iostream>
#include <vector>
int main()
{
vector<int> g1;
return 0;
}
DSA&C++
Functions:Access elements of the Vector
// C++ program to illustrate the
// element accessor in vector
1. reference operator [g] – Returns a #include <bits/stdc++.h>
using namespace std;
reference to the element at position
‘g’ in the vector int main()
{
2. at(g) – Returns a reference to the
vector<int> g1;
element at position ‘g’ in the vector
3. front() – Returns a reference to the for (int i = 1; i <= 10; i++)
g1.push_back(i * 10);
first element in the vector
4. back() – Returns a reference to the cout << "\nReference operator [g] : g1[2] = " << g1[2];
last element in the vector cout << "\nat : g1.at(4) = " << g1.at(4);
5. data() – Returns a direct pointer to
cout << "\nfront() : g1.front() = " << g1.front();
the memory array used internally by
the vector to store its owned cout << "\nback() : g1.back() = " << g1.back();
elements. // pointer to the first element
int* pos = g1.data();
DSA&C++
Output
The vector elements are: 10 10 10 10 10
The last element is: 15
The vector elements are: 10 10 10 10 10
The first element is: 5
The first element is: 10
The first element is: 5
The last element is: 20
Vector size after erase(): 0
Vector 1: 1 2
Vector 2: 3 4
After Swap
Vector 1: 3 4
Vector 2: 1 2
DSA&C++
Return an array using vectors
#include<iostream>
#include<vector>
using namespace std;
vector<int> reversingArray(int *scores,int n){
vector<int> reversedArray(n);
for(int i=0;i<n;i++){
reversedArray[i]=scores[n-1-i];
}
return reversedArray;
}
int main(){
int n=5;
int scores[n]={10,20,30,40,50};
vector<int> reversedScores=reversingArray(scores,n);
cout<<"The reversed array is";
for(int i=0;i<n;i++)
{
cout<<reversedScores[i]<<" ";
}
cout<<endl;
return 0;
return 0;
}
DSA&C++
Vector complexity
The time complexity for doing various operations on vectors is-
● Random access – constant O(1)
● Insertion or removal of elements at the end – constant O(1)
● Insertion or removal of elements – linear in the distance to the end of the
vector O(N)
● Knowing the size – constant O(1)
● Resizing the vector- Linear O(N)
DSA&C++