vector::at() in C++ STL Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vector at() is a built-in method used to access an element in a vector using index. It is the only access method that performs bound checking before accessing the element to confirm whether the given index lies is within the vector.Let’s take a quick look at a simple example that uses vector at() method: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 9}; // Accessing the element of second index cout << v.at(2); return 0; } Output4This article covers the syntax, usage, and common queries of vector at() method in C++:Table of ContentSyntax of vector at()Examples of vector at()Modifying Elements Using vector at()Catching Out-of-Range Exceptions with vector at()vector at() in C++ - FAQsSyntax of vector at()The vector at() is a member method of std::vector class defined inside <vector> header file.v.at(i);Parametersi: 0-based position of the element in the vector.Return ValueReturns the reference to the element at given index.If the index is out of bounds, an std::out_of_range exception is thrown.Examples of vector at()The below examples illustrate the common uses of vector at() method:Modifying Elements Using vector at() C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 9}; // Modify the element at 2nd index v.at(2) = 7; cout << v.at(2); return 0; } Output7Catching Out-of-Range Exceptions with vector at() C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 9}; try { // Attempting to access out of range index cout << v.at(5) << endl; } catch (const out_of_range& e) { cout << "Exception: " << e.what() << endl; } return 0; } OutputException: vector::_M_range_check: __n (which is 5) >= this->size() (which is 4) Comment More infoAdvertise with us A AyushSaxena Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library Practice Tags : CPPSTL Similar Reads Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Vector end() in C++ STL In C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr 3 min read vector rbegin() and rend() Functions in C++ STL In C++, std::vector::rbegin() and std::vector::rend() are built-in functions used to retrieve reverse iterators to the reverse beginning and reverse end of a vector. These functions allow easy traversal of vectors in reverse i.e. from the last element to the first. They are the member functions of t 3 min read vector :: cbegin() and vector :: cend() in C++ STL Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterat 2 min read Vector crend() in C++ STL In C++, the vector crend() is a built-in method used to obtain a constant reverse iterator pointing to the theoretical element just before the first element of the vector. It is used to mark the reverse end of the vector.Letâs take a look at an example: C++#include <bits/stdc++.h> using namesp 2 min read Vector max_size() in C++ STL In C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.Let's take a look at an example that illustrates this method:CPP#include <bits/stdc++.h 3 min read Vector capacity() in C++ STL In C++, the vector capacity() is a built-in method used to find the capacity of vector. The capacity indicates how many elements the vector can hold before it needs to reallocate additional memory. In this article, we will learn about vector capacity() method in C++.Letâs take a look at an example t 3 min read Vector resize() in C++ STL In C++, the vector resize() is a built-in method used to change the size of vector container after it is declared. It can be used to increase or decrease the size of vector.Letâs take a look at an example that illustrates the vector resize() method:C++#include <bits/stdc++.h> using namespace s 3 min read Vector empty() in C++ STL In C++, vector empty() is a built-in method used to check whether the given vector is empty or not. In this article, we will learn about vector empty() method in C++.Letâs take a look at an example that illustrates the vector empty() method:C++#include <bits/stdc++.h> using namespace std; int 2 min read Vector shrink_to_fit() in C++ STL In C++, vector shrink_to_fit() is a built-in function used to reduce the capacity of the vector to fit its size and destroys all elements beyond the size. In this article we will learn about vector shrink_to_fit() in C++.Letâs take a quick look at an example that illustrates vector shrink_to_fit() m 2 min read Like