How to Traverse a List with const_iterator in C++? Last Updated : 26 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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} Output: 10 20 30 40 50 Iterate a List with const_iterator in C++In C++, const_iterator provides a constant reference to the container elements. It means that we can only access the value but cannot modify it. It is useful to prevent unintended changes that may occur while traversing. To iterate a std::list with const_iterator in C++, we can use the std::list::cbegin() and std::list::cend() functions that give the constant iterator to the beginning and the end of the list. C++ Program to Iterate a List with const_iteratorThe following program illustrates how we can traverse a list with const iterator in C++: C++ // C++ program to illustrate how to traverse the list using // the const_iterator #include <iostream> #include <list> using namespace std; int main() { // Initializing a list list<int> myList = { 10, 20, 30, 40, 50 }; // Using const_iterator to iterate through the list // elements for (auto i = myList.cbegin(); i != myList.cend(); i++) { cout << *i << " "; } cout << endl; return 0; } Output10 20 30 40 50 Time complexity: O(N), where N is the number of elements in the list.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Traverse a List with const_iterator in C++? sravankumar_171fa07058 Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-list CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 List with reverse_iterator in C++? In C++, lists are containers that allow users to store data in non-contiguous memory locations. List provides reverse iterators that make it easy to traverse the list in reverse order. In this article, we will learn how to traverse a list with reverse_iterator in C++. Example Input: myList = {10,20, 2 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 Traverse a Set with reverse_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 provides reverse iterators that make it easier to traverse the set in reverse direction. In this article, we will discuss how to traverse a set with reverse_iter 2 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 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 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 Use Iterator with a Vector in C++? 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 2 min read How to Use a reverse_Iterator with a Map in C++? In C++, a reverse_iterator is a type of iterator that points to elements in a container in the reverse order. This means that using a reverse_iterator, you can traverse a container from end to beginning. In this article, we will learn how to use a reverse_iterator with a map in C++. Example: Input: 2 min read Const vs Regular iterators in C++ with examples Prerequisite: Iterators in STL Iterators are objects similar to pointers which are used to iterate over a sequence and manipulate the container elements. The advantage of using an iterator is that it reduces the lines of code to a single statement as they allow us to manipulate the built-in arrays i 3 min read Like