2

I'm trying to grasp the concept of the iterator. I've run some tests via the following code:

#include <iostream>
#include<map>
using namespace std;

int main(){
    map<string,string> mp;//create the map
    mp["key"]="value";//create a key/val pair

    map<string,string>::iterator it=mp.begin();//create an iterator named it


    cout<<(&it)<<" "<<(&*it)<<endl;// 0x7ffeeccd6a18 0x7f9dc5c05970
    it++;//moving the current value of the iterator
    cout<<(&it)<<" "<<(&*it);// 0x7ffeeccd6a18 0x7ffeeccd6a70
}

To my understanding, we can conceptualize an iterator as a box that holds values - values of the iterable we're iterating over. When we do "it++;" and move the current value of the iterator, we're now accessing a different element, and that's why (&*it) changes. The actual box doesn't change, and that's why (&it) stays the same in both cases (because we're getting the address of the iterator object).

I'm not sure if I understood this correctly, so please tell me if I'm right and correct me if I'm wrong.

discovery
  • 43
  • 6
  • Iterators are a concept. They can behave in different ways. The underlying implementation is just that, an underlying implementation. I can however not think of any example I've seen where your examples wouldn't hold up. – super Jul 06 '21 at 16:46
  • *"Values of the iterable"* is ambiguous. The iterator does *not* contain `key` nor `value`. It does contain the address of the element the iterator points to. – Beta Jul 06 '21 at 16:47
  • 1
    [What is an iterator in general?](https://stackoverflow.com/questions/51586495/what-is-an-iterator-in-general) – WhozCraig Jul 06 '21 at 16:51
  • 1
    Does this answer your question? [What is an iterator in general?](https://stackoverflow.com/questions/51586495/what-is-an-iterator-in-general) – Asesh Jul 06 '21 at 18:03

2 Answers2

5

What exactly is an iterator in C++?

Iterator is a concept. The concept describes a type with particular properties and operations with particular behaviours. A type that conforms to the "iterator" concept is said to be an iterator, and similarly objects of such type are also iterators.

More specifically, iterator is a generalisation of a pointer. It generally points to an entity (usually an object), and you can indirect through the iterator to access the pointed object (using the unary operator * which is called the indirection operator) and there is a way to modify the iterator to point to the "next" entity (using the operator ++).

we can conceptualize an iterator as a box that holds values

"holds" may be misleading. The referred entity isn't necessarily nor typically stored in the iterator. It is generally stored elsewhere and the iterator "knows" where the entity is.

Instead of a "box", I would describe it as a sign with with instructions on how to find the entity. As a visual metaphor, it "points" to the entity.

The actual box doesn't change, and that's why (&it) stays the same in both cases

To be clear, the value of it did change. But it is still the same iterator object, so its address is the same.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Yes, you are right. Iterators point to the current memory location of an element in the map. So, when you do mp.begin() it points to the first key value pair in the map. When you do it++ it starts pointing to next memory location, where the second key value pair lies.

Iterators are usually used to traverse over a map as follows:

    map<int, int> gquiz1;
  
    // insert elements in random order
    gquiz1.insert(pair<int, int>(1, 40));
    gquiz1.insert(pair<int, int>(2, 30));
    gquiz1.insert(pair<int, int>(3, 60));
    gquiz1.insert(pair<int, int>(4, 20));
    gquiz1.insert(pair<int, int>(5, 50));
    gquiz1.insert(pair<int, int>(6, 50));
    gquiz1.insert(pair<int, int>(7, 10));
  
    // printing map gquiz1
    map<int, int>::iterator itr;
    cout << "\nThe map gquiz1 is : \n";
    cout << "\tKEY\tELEMENT\n";
    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
        cout << '\t' << itr->first
             << '\t' << itr->second << '\n';
    }
    cout << endl;
Atharva Sharma
  • 295
  • 1
  • 9