Re: BinaryPredicate Question
In article <Xns98C4680FC6F84obrickermydejanewsco@216.196.97.136>,
Otis Bricker <obricker@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());
The following should compile fine:
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
struct DataItem
{
long ID_;
};
class IdLessThan: public binary_function<long, DataItem, bool>
{
public:
bool operator()( long lhs, const DataItem& rhs ) const {
return lhs < rhs.ID_;
}
};
int main()
{
long ID;
vector<DataItem> cache;
vector<DataItem>::iterator it =
upper_bound( cache.begin(), cache.end(), ID, IdLessThan() );
}
I ask because the compiler I am using is unable to compile the debug
build of this. It seems to be trying to test the predicate by calling:
IdLessThan::operator()(const DataItem& lhs,long rhs);
Is this version required or is it just a case of a debug version
requiring it for 'testing', which I believe can be disabled?
And would it be a good idea to include the extra form to allow the
testing by the debug build?
Adding the extra op() is better than not being able to compile in debug!
Maybe you should get a newer compiler instead (if you can.)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]