Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how the iterator in c++ could be printed?

Tags:

People also ask

How do I print an iterator?

So, the code would be: *it+=10; cout << *it << endl; I can print the address of both iterator and elements that are being iterated.

How do iterators work in C?

An iterator is an object that allows you to step through the contents of another object, by providing convenient operations for getting the first element, testing when you are done, and getting the next element if you are not. In C, we try to design iterators to have operations that fit well in the top of a for loop.

Is iterator in STL?

Iterators are one of the four pillars of the Standard Template Library or STL in C++. An iterator is used to point to the memory address of the STL container classes.


Suppose, I have declared a vector in C++ like this:

vector<int>numbers = {4,5,3,2,5,42}; 

I can iterate it through the following code:

for (vector<int>::iterator it = numbers.begin(); it!=numbers.end(); it++){     // code goes here } 

Now, I would talk about coding in the block of for loop.

I can access and change any value using this iterator. say, I want to increase every value by 10 and the print. So, the code would be:

*it+=10; cout << *it << endl; 

I can print the address of both iterator and elements that are being iterated.

Address of iterator can be printed by:

cout << &it << endl; 

Address of iterated elements can be printed by:

cout << &(*it) << endl; 

But why the iterator itself could not printed by doing the following?

cout << it <<endl; 

At first I thought the convention came from JAVA considering the security purpose. But if it is, then why I could print it's address?

However, Is there any other way to do this? If not, why?