vector at() in C++

at(): It returns the value at a given position in the vector.

Parameters:

pos: Position of the element to get from the vector.

Syntax:

vecName.at(pos)

Note:

If the position is greater than the number of elements of the vector it throws an exception

terminate called after throwing an instance of 'std::out_of_range'.

For Example: 

vec = {1, 2 , 3, 4}

vec.at(1) : It returns 2.

File: stl_vector.h

Example:

Output: 2

Approach

C++


#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intvec = {1234};
    cout << vec.at(1<< "\n";

    return 0;
}


No comments:

Post a Comment