Re: BinaryPredicate Question
"Greg Herlihy" <greghe@pacbell.net> wrote in
news:1169873749.305383.305950@v45g2000cwv.googlegroups.com:
On Jan 26, 3:14 pm, Otis Bricker <obric...@my-dejanews.com> wrote:
I'm trying to figure out is the following technique is valid.
Given
std::vector<DataItem> cache;
which is sorted by the ID_ field of each DataItem.
And this Predicate class:
class IdLessThan: public std::binary_function<long, DataItem, bool>
{
public:
bool operator()
( long lhs, const DataItem& rhs)const{return lhs <
rhs.ID_;};
};
Is the following valid?
Vector<DataItem>::iterator it =
std::upper_bound(cache.begin(),cache.end(),ID, IdLessThan());
No, the first parameter of the IdLessThan predicate function object
should be the dereferenced iterator value, while the second parameter
should be the comparison value (see ?25/8). In other words, the
current declaration has the parameters in the reverse of their expected
order.
I guess it would help to have a copy of the standard to review. The draft
version I usually reference online says:
"Returns: The furthermost iterator i in the range [first,last) such that
any iterator j in the range of [first,i) the following corresponding
conditions hold: !(value<*j) or comp(value,*j)==false"
The this param order, (long,const DataItem&) seems more in line with a
standard binary predicate like std::less.
And since the release build does not compile if I include only the (const
dataItem&,long) version, does that mean the compiler is non-conforming?
Guess I need to pop for a copy of the standard.
Thanks.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]