Reputation: 4328
Today i was trying to print elements of a set , Which went into an infinite loop . When i tried to fetch the size of set , it returned me correct value which was 8.
the code was like this
std::cout << "size of set is " << myset.size();
for(it=myset.begin();it!=myset.end();it++)
std::cout << "value is : " << *it;
iterator it was declared above before fetching the size . And one of the interesting fact was that one of the expected element was missing from set while printing it.
I know i would appear mad to you or making some silly mistake but my concern is that is it possible that myset.end() points to some random place ? But even though so why was it repeating the elements from set while printing and not crashing
Upvotes: 1
Views: 711
Reputation: 39254
The .end()
iterator returned by all STL containers points to one-past-the-end of the container, and dereferencing it (*iterator
) results in undefined behavior.
Your for loop will not dereference .end()
though as it stops when your iterator equals .end(), so this should not be a problem in the code you've posted. You probably had a different mistake in your other code earlier.
Upvotes: 1