![]() |
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++:
Output 10 20 30 40 50 Time complexity: O(N), where N is the number of elements in the list. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |