Vector At Function

C++ Vector at

It gives a reference to an element.

Syntax

Consider a vector v and k is the position. Syntax would be:

Example

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

Parameter

k : k defines the position of element which is to be returned by at function.

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 example, at function access the elements of vector .

Input Required

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