
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ vector::at() Function
The C++ vector::at() function provides a reference to the element that is present at the index specified as an parameter. The at() function throws an out-of-range exception if the position or index number is out of range and not present in the vector. The time complexity of the at() function is constant.
In C++, vectors are sequence containers that represent arrays with that can change their size during run-time. They employ contiguous storage locations for their elements just as efficiently as in arrays, therefore their elements can also be accessed using offsets on regular pointers to its elements.
Syntax
Following is the syntax for C++ vector::at() Function −
reference at (size_type n);const_reference at (size_type n) const;
Parameters
n − it indicates the position of an element in the container.if it is greaterthan or equal to vector size it throw exception.
Example 1
Let's consider the following example, where we are going to use the at() function and retrieving the output.
#include<iostream> #include<vector> using namespace std; int main(){ vector<int> tutorial{11,22,33,44}; for(int i=1; i<tutorial.size(); i++) cout<<tutorial.at(i); return 0; }
Output
When we compile and run the above program, this will produce the following result −
223344
Example 2
In the following example, we are going to read all the element in the vector.
#include<iostream> #include<vector> using namespace std; int main(){ vector tutorial = {"TutorialsPoint","TP","Tutorix"}; for(int i=0; i<tutorial.size(); i++){ cout<< tutorial.at(i) << "\n"; } cout<< "\n"; return 0; }
Output
On running the above program, it will produce the following result −
TutorialsPoint TP Tutorix
Example 3
Considering the following example, where we are going to calculate the subtraction of the vector values.
#include <iostream> #include <vector> using namespace std; int main (){ vector<int>tutorial {2,4,6,8,1,3,5,7}; int sub = 0; cout<< "Values:\n"; for (int i=0; i<tutorial.size(); i++) cout<< ' ' << tutorial.at(i); cout<< '\n'; for (int i=0; i<tutorial.size(); i++) sub -= tutorial.at(i); cout<< "Subtraction of all the values is : " << sub << "\n"; return 0; }
Output
Let us compile and run the above program, it will produce the following result −
Values: 2 4 6 8 1 3 5 7 Subtraction of all the values is : -36
Example 4
Following is the another scenario, where we are going to change the vector value by using the at() function.
#include <iostream> #include <vector> using namespace std; int main(){ vector<int> tutorial {22,88,66,55,44}; cout << "Declared Vector: "; for (const int& i : tutorial) { cout << i << " "; } tutorial.at(2) = 77; tutorial.at(0) = 99; cout << "\nModified Vector: "; for (const int& i : tutorial) { cout << i << " "; } return 0; }
Output
On running the above program, it will produce the following result −
Declared Vector: 22 88 66 55 44 Modified Vector: 99 88 77 55 44
Example 5
Let's consider the following example, which throws an error on passing the 'age.at(3) = 5' which is out of the range in the declared vector.
#include <iostream> #include <vector> using namespace std; int main(){ vector<int> age = {2,3,4}; age.at(3) = 5; for(int i = 0; i < age.size() ; i++) { cout << "age[" << i << "] = " << age.at(i) << endl; } return 0; }
Output
When we execute the above program, it will produce the following result −
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)