0

I'm learning c++ newly, and can't figure out the iterator... for instance, I have the code below with a map and, despite research, can't figure out the .end(), iterator it as well as first

   std::map<int, double> e;
    e[1] = 10.0;
    e[2] = 20.0;
    e[3] = 30.0;
    e[4] = 40.0;
    map<int, double>::iterator it;
    
    it = e.end();
    it--;
    int d = 1.0*it->first / 50000;

Could someone indicates me the meaning of each step of this code, from the map initialization to the last line ! Thanks !

Antoine
  • 37
  • 6
  • 2
    Without some explanation on your end of the research you've done and what specifically you don't understand, it'd be at best guesswork for someone to explain iterators in a way that you understand that's better than whatever way you've already seen them explained. – Nathan Pierson Jun 27 '22 at 04:50
  • 1
    @Anoop don't be so quick closing a question. It is not quite a duplicate. OP is asking for line by line explanation and beginner questions are fine and we should be able to answer them in detail. – Pepijn Kramer Jun 27 '22 at 05:26
  • 2
    Here is your code with some comments : [compiler explorer](https://godbolt.org/z/bMv9hrf14). This might also help you understand a bit more : https://www.learncpp.com/cpp-tutorial/introduction-to-iterators/ – Pepijn Kramer Jun 27 '22 at 05:28
  • @PepijnKramer I disagree. For clearing the basics one should refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of directly asking a question here on SO. – Jason Jun 27 '22 at 05:29
  • @PepijnKramer Moreover, the title of the question asked about `end()` while in the description OP is asking for the explanation of each line of the example. As i said, a question should be specific instead of asking a line by line explanation of the whole shown code. For a line by line explanation a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should be referred to clear the basics. And even if this question is specific about `end()`, it is still a duplicate. – Jason Jun 27 '22 at 05:34
  • @AnoopRana I think you are being too rigid, at least the OP provided code he worked on himself. Also consider yourself an absolute beginner again... it is hard to figure out what information to get from where. And don't close issues on your own, it should take at least 3 of us to do that. (This is where I stop discussing this) – Pepijn Kramer Jun 27 '22 at 05:49
  • Hi ! Thanks for your comment, I take great consideration of it. I apologize for who think my question shouldn't have been posted. In total honesty, I have done research before asking it, but what I can struggle with as a beginner, is to be able to understand some answer of already posted related question, so even though there is content, things are explained too fast or shallow. But I will try harder next time. Meanwhile, thank you ! – Antoine Jun 27 '22 at 05:56
  • Aside: the `/ 50000` is hiding a lot of what is going on, because `d` would be `0` with any of those elements – Caleth Jun 27 '22 at 11:04
  • @PepijnKramer This is not about being too rigid. If you allow this then practically many other worse question are also automatically allowed. For example [this](https://stackoverflow.com/questions/72772996/use-of-undeclared-identifier-x) or [this](https://stackoverflow.com/questions/72770658/what-causes-this-error-segmentation-fault-core-dumped) would then also be ok. As you can see in the above linked question, OP just pasted a code snippet and expecteing people to explain what the problem is. This is essentially what is done in this current question also. – Jason Jun 27 '22 at 13:41
  • @PepijnKramer That is, you can't just copy/paste some code snippet and expect people to explain it line by line on SO. – Jason Jun 27 '22 at 13:44

2 Answers2

2

Iterators are used to point at the memory addresses of STL containers.

.begin() points to the first element of container address.

.end() points to the last element of container address + 1.

this link explain iterator : iterator

1

For simplicity, you can imagine a map using this visualization

[X,X,X,X,X,X,X]_
 A             B

map.begin() will return a pointer to point A

map.end() will return a pointer to point B

An element in a map is a pair of (key, value). Thus, first refers to the key.

All elements in a map is sorted based on the key. Thus, your code is trying to get the maximum key divided by 50000

  • 2
    `map.begin()` and `map.end()` do not return pointers. They return iterators. Although there is a relationship between pointers and iterators, they are not the same thing. – Peter Jun 27 '22 at 07:16