devxlogo

Const Iterators

Const Iterators

You can think of an iterator as a pointer to elements of a container. All STL (Standard Template Library) containers provide the basic iterator type along with its const counterpart. The const iterator of a container should be used when the elements accessed through it are not supposed to be modified, like in the following example:

 void main() {	string s ("hello world"); // s contains 11 characters 	string::const_iterator p = s.begin(); //p is a const iterator pointing to the first element of s 	size_t n = 0; 	while ( p < s.end() ) {   // a loop that count the number of chars in s	 n++;    p++;   // move to the next element of s	}	cout<
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist