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

Vector At Function

BLUF: Mastering Vector At 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 At Function

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

C++ Vector at

It gives a reference to an element.

Syntax

Consider a vector denoted as 'v' with 'k' representing the index position. The syntax is as follows:

Example

vector<object_type>  v;
v.at(k) ;

Parameter

k : k specifies the index of the element to be retrieved using the at method.

Return value

It returns the element of specified position.

The following illustration shows how at function works

If i=0 :

If i=3 :

Example

Let's see a simple example.

Example

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v1{1,2,3,4};
for(int i=0;i<v1.size();i++)
cout<<v1.at(i);
return 0; 
}

Output:

In this instance, the at method retrieves the elements of a 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