When should I use Vector at() instead of vector operator[]? Last Updated : 27 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vector provides two methods for accessing its elements using indexes: vector operator[] and vector at(). Although both methods provide similar functionality, but there are some differences between them.The following table lists the primary differences between the vector operator[] and vector at():Parametervector operator[]vector at()Bound CheckingIt does not perform bound checking.This method performs the bound checking.ExceptionThis method does not throw any exception, but shows an undefined behaviour on accessing the index which is out of bound.This method throws an exception std::out_of_range on accessing the index which is out of bound.PerformanceIt is faster as compared to vector at(), as it does not perform bound checking.It is slower in comparison to vector operator[], as it performs the bound checking.Use CaseUse vector operator[] when we are certain the index is within bounds and need maximum performance.Use vector at() when we want to ensure safe access and handle out-of-bounds errors explicitly.Syntaxv[i];v.at(i);From the above table, we can infer that,One should use vector at() instead of the vector [] operator in situations where safety and bounds checking are important.ExampleBelow program demonstrate the difference between vector operator[] and vector at(): C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 7, 9}; // Accessing the index which is in range cout << v[3] << endl; cout << v.at(3) << endl; // Accessing the index which is out of bound cout << v[7] << endl; try { cout << v.at(7) << endl; } catch (const out_of_range &e) { cout << "Exception: " << e.what() << endl; } return 0; } Output7 7 0 Exception: vector::_M_range_check: __n (which is 7) >= this->size() (which is 5) Explanation: When we try to access the index which is not out of bound, then there will be no difference between both the methods, but when we try to access that index which is out of bound then vector operator[] shows undefined behaviour and vector at() method throws an exception. Comment More infoAdvertise with us Next Article When should I use Vector at() instead of vector operator[]? A anmolpanxq Follow Improve Article Tags : C++ Programs C++ STL cpp-vector Practice Tags : CPPSTL Similar Reads When to Use Vector Instead of Array in C++? In C++, both arrays and vectors are used to store collections of data. Arrays are a basic fixed-size sequence of elements, whereas vectors are part of the C++ STL and offer dynamic sizing and more flexibility. There are some cases where using vectors can be more beneficial than using arrays. In this 4 min read How to Use Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 min read How to Extract a Subvector from a Vector in C++? A subvector is a continuous part from the original vector. In this article, we will learn how to extract the subvector from a vector in C++.The simplest method to extract the subvector form a vector is by using vector's range constructor. Letâs take a look at an example:C++#include <bits/stdc++.h 3 min read How to Iterate Through a Vector Without Using Iterators in C++? In this article, we will learn how to iterate through the vector without using iterator in C++.The most efficient method to iterate through the vector without using iterator is by using traditional for loop. It accesses all the elements using index starting from 0 to vector size() - 1. Letâs take a 2 min read How to Use a Range-Based for Loop with a Vector in C++? C++ introduced the range-based for loop as a convenient way to iterate over elements in a container. In this article, we'll look at how to iterate over a vector using a range-based loop in C++. Example Input:myVector: {1, 2, 3, 4, 5}Output:1 2 3 4 5Range-Based for Loop with Vector in C++The syntax o 1 min read How to Access Vector Methods from Pointer to Vector in C++? In C++, we can create a pointer that points to the object of vector type. In this article, we will learn how to access member functions of std::vector from a pointer to the vector in C++. Example Input:vec = {1, 2, 3, 4}vector<int>* ptr = vecOutput:ptr->at(2): 3Accessing std::vector Methods 2 min read How to Iterate Through a Vector in C++? Iterating or traversing a vector means accessing each element of the vector sequentially. In this article, we will learn different methods to iterate over a vector in C++.The simplest way to iterate a vector is by using a range based for loop. Let's take a look at a simple example that iterate the v 3 min read How to convert a Vector to Set in C++ This article shows how to convert a vector to a set in C++. Example: Input: vector = {1, 2, 3, 1, 1} Output: set = {1, 2, 3} Input: vector = {1, 2, 3, 3, 5}Output: set = {1, 2, 3, 5} Below are the various ways to do the required conversion: Method 1: Naive SolutionGet the vector to be converted.Crea 4 min read How to Find the Size of a Vector in Bytes in C++? In C++, Vectors are dynamic containers that can change their size during the insertion or deletion of elements. In this article, we will explore how we can find the size of the vector in bytes in C++. Example: Input:myVector = {10,20,30,40,50}Output:Size of the vector in bytes is : 20 bytesFind the 2 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read Like