How to Traverse Vector using const_iterator in C++? Last Updated : 03 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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} Output: result: {1, 2, 3}Iterating Over a Vector Using const_iterator in C++We can get the const_iterator to a std::vector container using the std::vector::cbegin() and std::vector::cend() functions that return a const_iterator pointing to the beginning and the end of the vector, respectively. We can use constant iterators when we do not want to modify the data in the vector or the vector itself is constant. C++ Program to Iterator Over a Vector Using const_iteratorThe below program illustrates how we can iterate over a vector using const_iterator in C++. C++ // C++ Program to illustrate how we can iterate over a // vector using const_iterator #include <iostream> #include <vector> using namespace std; int main() { // Creating a vector of int vector<int> myVector = { 1, 2, 3 }; // Declaring a const_iterator to the vector vector<int>::const_iterator it; // Displaying vector elements using const_iterator cout << "Vector Elements: "; for (it = myVector.cbegin(); it != myVector.cend(); ++it) { cout << *it << " "; } return 0; } OutputVector Elements: 1 2 3 Time Complexity: O(N), where N is the number of elements in the vector.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Traverse a Set with const_iterator in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Traverse Vector Using const_reverse_iterator in C++? In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato 2 min read How to Traverse a Set with const_iterator in C++? In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in 2 min read How to Traverse a Map Using Iterator in C++? In C++, maps are associative containers that store elements where each element has a key value and a mapped value. An iterator is a pointer-like object that allows traversing through the elements of a container like a map. In this article, we will learn how to traverse a map using an iterator in C++ 2 min read How to Traverse a List with const_iterator in C++? In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50} 2 min read How to Traverse a Vector using for_each Loop in C++? In C++, vectors are dynamic containers that can change their size automatically during the insertion and deletion of elements. In this article, we will learn how we can use a for_each loop to traverse elements of a vector in C++. Example Input: myVector = {1,2,3,4,5} Output: // Squared each element 2 min read 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 Like