Why index value

in this problem when we try to find 15 then as 15 not present so it should return an iterator to the end of set.
And when we print the value at that iterator shouldn’t it be any garbage value? Thanks in advance.

hi @himanshugupta8 please tell which question you are referring to

7 problem stl quiz I

hi @himanshugupta8 it is giving the “index” of s.end(), which is 6. You can add more elements to the set to verify this.

1 Like

When iterator points to s.start() and we do *iterator then it prints the value at the start right. lower_bound() returns an iterator points to s.end() so now when we do *iterator then why it is outputting the index which is 6 rather than the value at that index which we don’t know so garbage value

hi @himanshugupta8 I found this explanation regarding vectors, but I think it holds true for all such containers/iterators. Pay attention to the last line.

v.end() points to memory one element past the end of the vector. C++ allows you to store that value and compare it with other pointers in your vector. That’s why you can use it as a stopping point in your for loop.
C++ does not allow you to deference v.end() . That would be undefined behaviour.

@himanshugupta8 in the documentation of set.end(), http://www.cplusplus.com/reference/set/set/end/ again see the last part of this sentence.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.