0

I need to iterate over the entire map without stopping the loop. My example works but it uses two loops, can this be fixed?

I think it's possible to do this using only one for loop

#include <map>

map<int, int>map2;

    map2[1] = 11;
    map2[2] = 12;
    map2[3] = 13;

    for (;;)
    {
        for (auto& a : map2)
        {

            cout << a.first << " : " << a.second << '\n';
        }
    }
Fade
  • 85
  • 11
menfix
  • 61
  • 7

1 Answers1

2

Use the std::map::iterator. That way you can just check if the iterator is at the end and if so reset it to the beginning.

map<int, int>::iterator it;
for ( it = map2.begin(); it != map2.end(); it == std::prev( map2.end() ) ? it = map2.begin() : it++ )
{
    cout << it->first << " : " << it->second << '\n';
}

Clarification

it == std::prev( map2.end() ) ? it = map2.begin() : it++

That is the ternary operator. You first ask if the iterator is equal to the last element in the map. We add the std::prev() in order to get the last element as map::end() provides us with a past-the-end value.

it == std::prev(map2.end())

If it is the last element you set the iterator to the beginning of the map.

it = map2.begin()

Else the iterator is incremented and you get the next element

it++

See also: Loop through map

Fade
  • 85
  • 11
  • and this loop can capture map changes like mine? – menfix Sep 24 '19 at 05:55
  • Yes it detects whenever a new value is pushed into the map. The iterator method is more or less the same as `for(auto& a : map2)` with the addition of the iterator reset – Fade Sep 24 '19 at 05:58
  • For further reading on the difference between iterator and range based for: [Here](https://stackoverflow.com/questions/29578219/range-based-for-loop-vs-regular-iterator-for-loop) – Fade Sep 24 '19 at 06:03
  • Is it possible to shorten the cycle a little? he looks long, poorly readable. – menfix Sep 24 '19 at 06:12
  • 1
    Your original solution using two loops was more succinct and readable, that's why. Typically, you can either have readable, or "in one loop". – Tas Sep 24 '19 at 06:15
  • And what does mean? `prev(end(map2))` – menfix Sep 24 '19 at 06:50
  • Edited my answer for clarification – Fade Sep 24 '19 at 07:02
  • nothing if i delete? `it != map2.end(); `Or the loop may crash sometime because of this. – menfix Sep 24 '19 at 07:12
  • You could delete it but it would definetly hurt the readability even further. You could even do `#define mBegin it = map2.begin()` `#define mLoop it == std::prev( map2.end() ) ? it = map2.begin() : it++` and then `for ( mBegin;; mLoop )` – Fade Sep 24 '19 at 07:20
  • Not thanks, `#define` is a bad idea, it is better not to use it, only very in extreme cases. Because of it, people commit very strong bugs in programs. – menfix Sep 24 '19 at 07:30