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.