How to Use Iterator with a Vector in C++? Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an iterator with a vector in C++. Iterator with a Vector in C++We can use the iterator with a vector in the same way as we can use the pointer with arrays. The std::vector member functions std::vector::begin() and std::vector::end() return the iterator to the beginning and the end respectively. We can then use the increment and decrement operations to move through the vector while accessing and manipulating the value of the elements. C++ Program to Traverse Vector with IteratorThe below example demonstrates how to use an iterator with a vector for traversal. C++ // C++ Program to show how use an Iterator to traverse a vector #include <iostream> #include <iterator> #include <vector> using namespace std; // Print vector function using iterators void printVector(vector<int>& v) { // Create iterator of vector<int> type vector<int>::iterator myItr; // Initialize iterator and begine traversal for (myItr = v.begin(); myItr != v.end(); myItr++) { cout << *myItr << " "; } cout << endl; } int main() { // Create and initialize a vector vector<int> v = { 1, 2, 3, 4, 5 }; // Print vector printVector(v); return 0; } Output1 2 3 4 5 Time Complexity: O(N), where N is the size of the vector.Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Use Iterator with a Vector in C++? D dotslash_adwitiya Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that 3 min read How to Use const_iterator with a Map in C++? In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{âappleâ, 1} 2 min read How to Iterate Through a Vector in C++? Iterating or traversing a vector means accessing each element of the vector sequentially. In this article, we will learn different methods to iterate over a vector in C++.The simplest way to iterate a vector is by using a range based for loop. Let's take a look at a simple example that iterate the v 3 min read How to Iterate Through a Vector Without Using Iterators in C++? In this article, we will learn how to iterate through the vector without using iterator in C++.The most efficient method to iterate through the vector without using iterator is by using traditional for loop. It accesses all the elements using index starting from 0 to vector size() - 1. Letâs take a 2 min read How to Reverse Iterate a Vector in C++? In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai 2 min read Like