mercredi 1 juin 2016

Valdidity of std::prev and std::next for std::lists

I am storing an iterator to a list

list<int> l;
l.push_back(21);l.push_back(1);l.push_back(31);l.push_back(41);

auto it = l.find(21);

Now, in my behavior, whenever I delete a node, I need to add up the adjoining elements. Something like this.

   auto prev = std::prev(it);
   auto next = std::next(it);
   *prev = *prev + *next;
   l.erase(it);

Now as you see, I need to ensure all boundary conditions. What values do std::prev() and std::next() return , if they are the first and last elements, or if "it" itself has become invalid at some point.

Aucun commentaire:

Enregistrer un commentaire