Following program hangs. I know, several ways to fix it by changing the code.
// How to compile
// % g++ <filename>.cpp
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> empty;
set<int>::iterator iter = empty.begin() ;
while (iter++ != empty.end())
{
cout << *iter << "\n";
}
return 0;
}
My questions are:
thank you in advance for the answers.
iter
already points to the end of the set. Incrementing it further with iter++
is not allowed. The workaround is to write a loop that can deal with an empty range:
for (auto &it : empty)
for (; iter != empty.end(); ++iter)