https://www.geeksforgeeks.org/set-lower_bound-function-in-c-stl/ But as here it is written and also told in the lectures that…"If the key passed in the parameter exceeds the maximum value in the container, then the iterator returned is equivalent to s.end() "…and we dont know what is the value at s.end()… so it should be garbage value…It is nowhere written that if key is maxm than largest element…it will return size of array
Question 7 why is ans 6?
it has been observed that the size of the container is returned
that is equal to s.end()
because if u think of the sort or other stl function where we try to send start and end pointer
then
sort(s.begin() , s.end())
which is equivalent to
sort( s , s+n)
where n is the size of array
so s.end == s+n
so end == n
hence 'end of container means returning size of array ( ie n )
I still don’t get it…
auto it = s.lower_bound(15);
cout<< *it <<endl;
If it was cout<< it-arr <<endl;
Then 6 should be printed. Is there some misprint in my question?
Q7. stl lower_bound in set
What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
set<int>s;
s.insert(1);
s.insert(5);
s.insert(7);
s.insert(2);
s.insert(12);
s.insert(10);
auto it = s.lower_bound(15);
cout<< *it <<endl;
return 0;
}
15
5
6
Any garbage value
i assume that u are not understanding why *it is giving 6
this instead should give 6
but the auto iterator if found a value within s.begin() and s.end () then will return the value of the element else
internally it is defined in such a way that it returns the size of container
the internal definition has the upper hand in deciding the output.
