How to write a compare function for lowerbound ?

I have a vector of objects.
The object has an int 'id' prop, and the vector is ordered by id's
I want to find a lower_bound for 33 (in example).
And I dont know how to write the compare element...

lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );


Any help would be appreciated.
Last edited on
I've never had the need to do this, however I find it interesting that any question asked by anyone with more than 200 posts is generally ignored.

With some quick searching here's what I found:
http://blogginman.blogspot.com/2005/09/c-tip-lowerbound-and-functors.html
http://stackoverflow.com/questions/869593/compare-function-for-upper-bound-lower-bound

If you require more help I'm willing, but I've got to get to work, sorry good luck.
Hello,

sorry to ask again (I am not sure I can help you) but what do you mean by "lowerbound" exactly?

I guess (through googling) that you mean you want to find the first element in the sorted range [first,last) which does not compare less than value, correct?

Well the only you have to do is implement a bool function that will implement the comparison you want. Normally is operator< but I guess with a cmp you could use something else:

bool cmp(ForwardIterator current, const T& value)
{
return *current < value;
}

I checked:

http://www.cplusplus.com/reference/algorithm/lower_bound/

for the idea and instead of *it<value I would use the bool cmp. Of course you can make another function comparing some different member of the object.
Thanks ultifinitus. Ths stackoverflow proposition is what I'm looking for...
What do you mean with 'I find it interesting that any question asked by anyone with more than 200 posts is generally ignored.'
Thanks any way
Topic archived. No new replies allowed.