Search code examples
c++c++11iterationmultimap

iterating over a group in multimap how do I know if I am in first or last elements?


I mean this situation:

for (auto iter = myMmap.equal_range(find_key).first;
  iter != myMmap.equal_range(find_key).second;
  ++iter)
{
  //code
}

How to derive from the iterator (that is without using counters) that I'm in first or last item in this set.


Solution

  • Like this?

    auto range = myMmap.equal_range(find_key);
    
    for (auto it = range.first; it != range.second; ++it) {
        if (it == range.first) { // first
    
        } else if (std::next(it) == range.second) { // last
    
        } else {}
    }