Vector Operator Function

C++ Vector operator

This function is used to access a specified element.

Syntax

Consider a vector 'v' and position 'pos'. Syntax would be:

Example

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.

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:

Output

C C++ java

In this example,each element is accessd using operator function.

Example 2

Let's see simple example

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:

Output

1 2 3 4 5

In this example, each element of vector v is accessed using operator function.

Input Required

This code uses input(). Please provide values below: