Here according to me output should be 7,200.
But it is showing 8,500.
In compartor function , ‘a’ is money or ‘b’ is money…
Please Clarify
Here according to me output should be 7,200.
But it is showing 8,500.
In compartor function , ‘a’ is money or ‘b’ is money…
Please Clarify
@ashishnnnnn b is money in the compare function.
Using this comparator your function is behaving like upper_bound function, ie it will return the first value that is strictly greater than the key(money in this case)
so 500 is the first value that is strictly greater than 200 and hence iterarator to 500 is returned. Subtract 1 from the index to get 200
Hii…Can you explain how this is behaving as upper bound.
hi @ashishnnnnn please refer to this At time-5:33 cannot understand compare
Let me know if you have any confusion
As you explained by taking each coin… Does it mean comparator will work till it return false
@ashishnnnnn yes that is correct. Although it is not necessary that all the values will be checked. The STL function needs a sorted array so it probably uses a variation of binary search.
Please explain this https://onlinegdb.com/Hk2U8r6kP …Beacuse here comparator is still working even it return false.
Here lower bound is working on binary search not linear search…The way you explained
I know it will take time But please could you ecplain using this code… https://onlinegdb.com/Hk2U8r6kP
@ashishnnnnn there is no need to get confused with the inner working of STL function as they can be very complicated. The important thing here to understand is that when it returns greater than or equal value, and when it returns strictly greater value…is that part clear?
Okay can you explain the working of this code… Here i have used upper_bound with a<b
@ashishnnnnn you used a < b so now it returned the value that is greater or equal to money. money = 200 so iterator to 200 is returned. (200 >= 200)
If you run this program without compare function, the output will be 500 because then the value strictly greater than 200 will be returned. (500 > 200)
Here value is being returned greater than 200 (money) even i used a<b
Please note here i have used upper bound function
@ashishnnnnn look at this code https://onlinegdb.com/rkwRMI6Jv now a and b are exchanged you can say (in the compare function). money is a now, in the upperbound function
This is confusing… How will i come to know which is “a” and which is “b” in compare function
As a is money so in first comparision a(200) is less than 1, it should return 0 index