In C++ the vector class provides a function at() to return the value at the given index position. Let us see the syntax of the function,
Table Of Contents
Syntax of vector::at() function
reference at (size_type n); const_reference at (size_type n) const;
It accepts an index position “n”, as an argument and returns the reference to the element at that given index position. It also checks for the boundaries; it means if the given index position is greater than the size of the vector, then it will throw and out_of_range
exception.
Parameters
n
: The Index position of the element, that needs to be fetched.
Returns : The reference of an element at the given index position.
Let us see some examples,
Frequently Asked:
- vector::clear() function in C++
- vector::insert() function in C++
- vector::push_back() function in C++
- vector::erase() function in C++
Example of vector::at()
In this example, we will create a vector of five integers, and then we will use the at() function to fetch the element at the index position 3. The indexing in C++ start from zero. It means, the (i+1)th
element in vector has index position i
. So, to fetch 4th element of vector, we need to get the element at index position 3. Let’s see the example,
#include <iostream> #include <vector> int main () { // create a vector, and initialize with five integers std::vector<int> vectorObj {11, 12, 13, 14, 15}; // Get 4th element of vector // i.e. element at index position 3 int elem = vectorObj.at(3); std::cout<< elem << std::endl; return 0; }
Output:
14
It returned the value at index position 3 in the vector.
vector::at() and out_of_range exception
The vector::at()
function can throw out_of_range
exception, if the given index position is out of bounds. It means, if the given index position is either negative, or greater than the size of the vector, then the at()
function can raise out_of_range
exception. Let’s see an example,
#include <iostream> #include <vector> int main () { // create a vector, and initialize with five integers std::vector<int> vectorObj {11, 12, 13, 14, 15}; // Get element at index position 21 int elem = vectorObj.at(21); std::cout<< elem << std::endl; return 0; }
Output:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 21) >= this->size() (which is 5) Aborted (core dumped)
As the vector size was 5 only, and we tried to access the element at index poistion 21, therefore it threw std::out_of_range exception.
So, to avoid this kind of scenarios, we should the try catch with vector::at() function. Let’s see an example,
#include <iostream> #include <vector> int main () { // create a vector, and initialize with five integers std::vector<int> vectorObj {11, 12, 13, 14, 15}; try { // Get element at index position 21 int elem = vectorObj.at(21); std::cout<< elem << std::endl; } catch(const std::out_of_range& e) { std::cerr << e.what() << '\n'; } std::cout<<"program ends here. \n"; return 0; }
Output:
vector::_M_range_check: __n (which is 21) >= this->size() (which is 5) program ends here.
Here we safely handeled the exception scenaio.
Update vector using vector::at()
The vector::at()
function returns a reference of the element at given index. So, we can modify the element value in vector, using the reference returned by the at()
function. Let’s see an example,
#include <iostream> #include <vector> int main () { // create a vector, and initialize with five integers std::vector<int> vectorObj {11, 12, 13, 14, 15}; // Update value at index position 2 vectorObj.at(2) = 100; for (size_t i = 0; i < vectorObj.size(); i++) { std::cout<< vectorObj[i]<<", "; } std::cout<<std::endl; return 0; }
Output:
11, 12, 100, 14, 15,
We changed the third value of vector from 13 to 100 using the vector::at()
function.
Summary
We learned how to use the vector::at()
function in C++. Thanks.