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 A AyushSaxena Follow Improve A AyushSaxena Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++4 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like