Set stl doubt on s.end()


the given code is returning 5 but shouldn’t it return 6 as 6 is the last element of the set

@nischay1111 s.end() is actually last position in set + 1
so in order to access last element you have to do -
auto it=s.end();
it–;
cout<<*it;
i hope its cleared if yes dont forget to hit like and mark resolved :smiley:

but 5 is not at last position +1 of my set
Moreover if i do it-- or it++ , both are returning 6
Please clarify

@nischay1111 you cant access anything outside set it will give garbage. auto it=s.end() it will also give garbage as its outside the set . your last position is it-1 where it = s.end()

for(it=s.begin() ; it!=s.end() ; it++)cout<<*it<<" ";
see this is how you print the set you are going upto 1 less than s.end()