How to Access the First Element of a Vector in C++? Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 5, 9, 7}; // Accessing the first element cout << v.front(); return 0; } Output3Explanation: The front() function returns the reference to the first element of vector.C++ also provides some other methods to access the first element of vector. They are as follows:Table of ContentUsing Vector [] OperatorUsing Vector at()Using Vector begin()Using Vector rend()Using Vector [] OperatorThe index of the first element in vector is 0, so we can access element at first element by index using vector operator []. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 5, 9, 7}; // Accessing the first element cout << v[0]; return 0; } Output3Using Vector at()The vector at() method can also be used to access the first element of the vector by using its index. The advantage of this method is that if the vector is empty then it throws std::out_of_range exception. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 5, 9, 7}; // Accessing the first element cout << v.at(0); return 0; } Output3Using Vector begin()The vector begin() method returns an iterator pointing to the first element of vector which can be then accessed by dereferencing the iterator. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 5, 9, 7}; // Accessing the first element cout << *v.begin(); return 0; } Output3Using Vector rend()The vector rend() is an iterator pointing to theoretical element that is just before the first element of vector. On decrementing this iterator by 1, it will point to first element of vector which can be then accessed by dereferencing the iterator. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 5, 9, 7}; // Accessing the first element cout << *--v.rend(); return 0; } Output3 Comment More infoAdvertise with us Next Article How to Access the First Element of a Vector in C++? anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Access the First Element of a Deque in C++? In C++, deques also called double-ended queues are sequence containers that can perform insertions and deletions on both ends. In this article, we will learn how to access the first element of a deque in C++. Example: Input: myDeque = {1,2,3,4,5} Output: Deque Elements: 1 2 3 4 5 First Element: 1Acc 2 min read How to Access Elements in a Vector in C++? In C++, vector provides fast access to its elements using their position as indexes. In this article, we will discuss different ways to access the element in vector using index in C++.The simplest method to access the elements of vector is by using vector operator [] with the corresponding index val 3 min read How to Access First Element in a Vector Using Iterator in C++? In C++, vectors are containers similar to arrays but unlike arrays, vectors can resize themselves during the runtime. These vectors provide iterators to access their elements. In this article, we will learn how to access the first element in a vector using an iterator in C++. Example Input:myVector 2 min read How to Access First Element of a List in C++? In C++, a list is a sequential container that allows constant time insertions and deletions and allows the users to store data in non-contiguous memory locations. In this article, we will learn how to access the first element in the list in C++. Examples: Input: myList ={1, 2, 3, 4, 5} Output: The f 2 min read How to Access the Last Element in a Vector in C++? Given a vector of n elements, the task is to access the last element in C++.ExamplesInput: v = {11, 23, 9, 7};Output: 7Explanation: Since 7 is the last element of the vector.Input: v = {1, 3, 11, 52};Output: 52Explanation: Since 52 is the last element of the vector.Following are the different ways f 2 min read How to Access the Top Element of a Stack in C++? In C++, The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure in which insertion and deletion are done at the end only. In this article, we will learn how to access the top element of a stack in C++. Example: Input: myStack = {10, 20, 30, 40} Output: Top El 2 min read How to Add Element at the End of a Vector in C++? Vector allows fast insertion and deletion at the end. In this article, we will learn different ways to add an element at the end of the vector.The easiest way to add an element at the end of vector is by using vector push_back() function. Letâs take a look at a simple example:C++#include <bits/st 4 min read How to Access Elements in a Vector of Char Arrays? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to access elements in a vector of char arrays in C++. Example: Input: myVector = {âappleâ, âbananaâ, âcherryâ} Outpu 2 min read How to Access Elements of a Pair in C++? In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First El 2 min read How to Find the First Occurrence of an Element in a Vector? In C++, the vector is a dynamic array that is defined in the STL template library inside the <vector> header. In this article, we will learn how to find the first occurrence of a specific element in a vector in C++. For Example Input: vector<int>v = {5, 7, 1, 2, 3, 7, 1} Target = 1 Outpu 2 min read Like