Re: BinaryPredicate Question
Daniel T. wrote:
"James Kanze" <james.kanze@gmail.com> wrote:
Daniel T. wrote:
"James Kanze" <james.kanze@gmail.com> wrote:
Otis Bricker 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. Given this call, IdLessThan must be callable with a first
argument of type DataItem, and a second of type long (and the
reverse).
Several people have said this now, but the OPs code works fine in gcc
and at http://www.comeaucomputing.com/tryitout/.
Also, Dikumware's and SGI's documentation say that upper_bound only uses
pred(x, y) where x is the 3rd argument passed in and y is the elements
in the container.
What am I missing?
Are you sure.
Yes.
I just looked at the Dinkumware site, and the
documentation for lower_bound...
Isn't relevant because we are talking about upper_bound, not lower_bound.
Which explains the confusion.
The current draft of the standard is contradictory here. In the
requirements specifications of algorithms (which I quoted
elsewhere), it explicitly says that the iterator will always be
on the right; in the documentation of upper_bound, however, it
speaks of "!(comp(value, e)", where e corresponds to something
dereferenced by the iterator.
(Come to think about it: in the most general
cases---equal_range, for example---, you need all four
operators: <, >, <=, and >=. To get them from just one
operator---and !---you need to be able to call that operator in
both directions. So I suspect that the text in the current
draft still needs some work.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]