I am trying to erase the element that I just traversed into. I initially forgot to store the return value of s1.erase(... inside the for loop to ultimately set the condition to exit the loop. With the way the code is right now, I expected the loop to continue indefinitely. But it works just the way it was originally intended to work. It looks like std::erase advances the iterator and stores the value in rit. I am not able to find any documentation that explains this behavior.
https://en.cppreference.com/w/cpp/container/set/erase says that the iterator returned has to be stored. All arguments of the set::erase are passed by value, so how is the reverse iterator being advanced?
How is this loop completing?
std::set<int> s1;
s1.insert(20);
s1.insert(30);
s1.insert(50);
auto rit = s1.rbegin();
for (; rit!= s1.rend();)
{
std::cout << "rit is " << *rit << " size is " << s1.size() << std::endl;
s1.erase(std::next(rit).base());
std::cout << "rit after erase is " << *rit << std::endl;
}
The output is
rit is 50 size is 3
rit after erase is 30
rit is 30 size is 2
rit after erase is 20
rit is 20 size is 1
Segmentation fault