In C++-STL, set::end() returns an iterator pointing to past-the-last element of the set container. Since it does not refer to a valid element, it cannot de-referenced end() function returns a bidirectional iterator.
But when I execute the following code:
set<int> s;
s.insert(1);
s.insert(4);
s.insert(2);
// iterator pointing to the end
auto pos2 = s.end();
cout<<*pos2;
it prints 3
as output. The output increases as I insert more elements to the set and is always equal to the total number of elements in the set.
Why does this happen?