This function is utilized to retrieve a designated element.
Syntax
Consider a vector 'v' and position 'pos'. The syntax is as follows:
v.operator[ ](pos);
Parameter
pos : It defines the position of the element.
Return value
It returns the element of specified location.
Example 1
Let's see a simple example.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"C","C++","java"};
for(int i=0;i<v.size();i++)
cout<<v.operator[](i)<<" ";
return 0;
}
Output:
C C++ java
In this instance, every component is retrieved using the operator method.
Example 2
Let's see simple example
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
for(int i=0;i<v.size();i++)
cout<<v.operator[](i)<<" ";
return 0;
}
Output:
1 2 3 4 5
When working with vectors, the operator function is employed to access individual elements within the vector.