Vector back() in C++ STL Last Updated : 21 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the vector back() is a built-in function used to retrieve the last element of the vector. It provides a reference to the last element which allows us to read or modify it directly.Let’s take a quick look at a simple example that illustrates the vector back() method: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {5, 10, 15, 20}; // Accessing the last element of vector v cout << v.back(); return 0; } Output20This article covers the syntax, usage, and common examples about the vector back() method in C++ STL:Table of ContentSyntax of Vector back()Examples of vector::back()Modify the Last Element of the VectorBehaviour of vector::back() with Empty VectorsDifference Between Vector back() and end()Syntax of Vector back()The vector back() is a member method of std::vector class defined inside <vector> header file.v.back();Parameters:This function does not take any parameters.Return Value:Returns the reference to the last element of the vector container if present.If the vector is empty, then the behaviour is undefined.Examples of Vector back()The below examples illustrate how to use vector back for different purposes and in different situation:Modify the Last Element of the Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {5, 10, 15, 20}; // Modify the last element v.back() = 50; for (int i : v) cout << i << " "; return 0; } Output5 10 15 50 Behaviour of Vector back() with Empty Vectors C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Checking whether a vector is empty before using // vector back() method if (!v.empty()) { cout << v.back(); } else { cout << "Vector is empty!"; } return 0; } OutputVector is empty!Explanation: Calling back() on an empty vector causes undefined behaviour. To prevent this, always check if the vector is not empty.Difference Between Vector back() and end()The vector back() and vector end() can both be used in to access the last element of the vector, but they have some differences:FeatureVector back()Vector end()PurposeReturns a reference to the last element of the vector.Returns an iterator pointing to one past the last element.Return TypeReference (T&) or constant reference (const T&).vector<T>::iterator type.UsageOnly used for direct access to the last element.Can be used for iteration and access.Syntaxv.back();*(v.end() - 1);In Short,Use back() for quick, direct access to the last element.Use end() when working with iterators or algorithms. Comment More infoAdvertise with us Next Article Vector back() in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library Practice Tags : CPPSTL Similar Reads Vector pop_back() in C++ STL In C++, the vector pop_back() is a built-in method used to remove the last element from a vector. It reduces the size of the vector by one, but the capacity remains unchanged.Letâs take a look at an example that illustrates the vector pop_back() method:C++#include <bits/stdc++.h> using namespa 3 min read Vector push_back() in C++ STL In C++, the vector push_back() is a built-in method used to add a new element at the end of the vector. It automatically resizes the vector if there is not enough space to accommodate the new element.Letâs take a look at an example that illustrates the vector push_back() method:C++#include <bits/ 2 min read vector::at() in C++ STL 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 2 min read 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 begin() in C++ STL In C++, the vector begin() is a built-in method used to obtain an iterator pointing to the start of the vector. This iterator is used to traverse the vector or perform operations starting from the beginning of the vector.Letâs take a look at an example that illustrates the use of the vector begin() 4 min read Like