How to Access First Element in a Vector Using Iterator in C++? Last Updated : 18 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 = {10,20,30,40,50}Output:The first element of the vector is : 10Retrieving First Element of a Vector Using Iterator in C++To access the first element of a std::vector using an iterator, we can use the std::vector::begin() function which returns the iterator to the first element in the vector. Then we can dereference this iterator using the dereferencing (*) operator to get the value of the first element stored in the vector. C++ Program to Retrieve First Element of a Vector Using IteratorThe following program illustrates how we can access the first element in a vector using an iterator in C++. C++ // C++ Program to how to access the first element in a // vector using an iterator #include <iostream> #include <vector> using namespace std; int main() { // Initializing a vector vector<int> vec = { 10, 20, 30, 40, 50 }; // Obtain an iterator pointing to the beginning of the // vector vector<int>::iterator it = vec.begin(); // Access the first element of the vector using the // iterator int first_element = *it; // Print the first element of the vector cout << "The first element of the vector is: " << first_element << endl; return 0; } OutputThe first element of the vector is: 10 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access First Element in a Vector Using Iterator in C++? vishaldhaygude01 Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Access Vector Element Using Iterator in C++? In C++, vectors are container that works like a dynamic array. STL provides iterators for the traversal of these containers. In this article, we will learn how to access an element in a vector using an iterator in C++. Example Input: myVector = {1, 5, 8, 1, 3, 4} Output: myVector value at index 5 us 2 min read How to Access the First Element of a Vector in C++? 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 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 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 Value in a Map Using Iterator in C++? In C++, a map is a container that stores elements in the form of a key value and a mapped value pair. In this article, we will learn how to access a value in a map using an iterator in C++. Accessing Value in a Map Using Iterator in C++To access a value associated with a key in a std::map using an i 2 min read How to Find Sum of All Elements of a Vector using STL in C++? In this article, we will find the sum of all the elements of the vector using STL in C++.The simplest method to find the sum of all elements of vector using STL is accumulate() method. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector 2 min read 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 Find the Maximum Element of a Vector using STL in C++? Given a vector, find the maximum element of the vector using STL in C++. ExampleInput: v = {2, 4, 1, 5, 3}Output: 5Explanation: 5 is the largest element of vector.Input: v = {11, 23, 3, 5, 24}Output: 24Explanation: 24 is the largest element of the given range.STL provides the following different met 3 min read How to Traverse Vector using const_iterator in C++? In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out 2 min read Like