Vector Operator Function - C++ Programming Tutorial
C++ Course / STL Vector / Vector Operator Function

Vector Operator Function

BLUF: Mastering Vector Operator Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Vector Operator Function

C++ is renowned for its efficiency. Learn how Vector Operator Function enables low-level control and high-performance computing in the tutorial below.

This function is utilized to retrieve a designated element.

Syntax

Consider a vector 'v' and position 'pos'. The syntax is as follows:

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 instance, every component is retrieved using the operator method.

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

When working with vectors, the operator function is employed to access individual elements within the vector.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience