Problem with std::map

Revision en1, by SPyofgame, 2020-04-22 15:39:01

Recently while I am solving a problem, I realised that increase iterator after erasing the last element of the map (the map is empty) then it would run infinitively

So I stop the loop by adding a small line to check whether it is empty or not

/// map-looping using either iterator or reverse iterator
{
    /// some code upper that erase the element but possible to make the map empty
    if (map.empty()) break;
}

Is there an alternative way (some other implementations) to prevent from iterator increament when the map is empty (and reverse_iterator too if possible)

Tags #asking, #data_structure

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English SPyofgame 2020-04-22 15:39:01 614 Initial revision (published)