Signature of the predicate in std::lower_bound



 DEVELOP > c-Plus-Plus > Signature of the predicate in std::lower_bound

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 24 Aug 2007 01:30:37 AM
Object: Signature of the predicate in std::lower_bound
I am using the following code:
#include <vector>
#include <algorithm>
class A
{
};
bool comparator( A * b, const char * a )
{
// return appropriate true/false value
}
int main( int argc, char * argv[] )
{
std::vector< A * > vt;
const char * p = "a";
std::lower_bound( vt.begin(), vt.end(), p, comparator );
return 0;
}
Note that the second argument of the comparator function is the same
as 'p', the 'object' that is being provided. Is this a standard
compliant technique? If not, shouldn't the standard allow this? It
comes in useful in a LOT of situations. (Incase you are wondering,
this worked perfectly on Visual Studio 2003 but doesn't work on Visual
Studio 2005 with SP1).
.

User: "Victor Bazarov"

Title: Re: Signature of the predicate in std::lower_bound 24 Aug 2007 07:27:24 AM
wrote:

I am using the following code:

#include <vector>
#include <algorithm>

class A
{
};

bool comparator( A * b, const char * a )
{
// return appropriate true/false value
}

int main( int argc, char * argv[] )
{
std::vector< A * > vt;
const char * p = "a";

std::lower_bound( vt.begin(), vt.end(), p, comparator );

return 0;
}

Note that the second argument of the comparator function is the same
as 'p', the 'object' that is being provided. Is this a standard
compliant technique? If not, shouldn't the standard allow this? It
comes in useful in a LOT of situations. (Incase you are wondering,
this worked perfectly on Visual Studio 2003 but doesn't work on Visual
Studio 2005 with SP1).

I couldn't find any direct set of requirements for the 'Compare' argument
of 'lower_bound' template except that the container "should be partitioned
with respect to 'comp(e, value)'", where 'comp' is your 'comparator'.
Whether this requires the 'comparator' to be callable with 'a' and 'b'
reversed (that's what Visual C++ requires, and that's why it fails) is
open to interpretation.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "Richard Herring"

Title: Re: Signature of the predicate in std::lower_bound 24 Aug 2007 07:51:38 AM
In message <famirb$bsv$1@news.datemas.de>, Victor Bazarov
<v.Abazarov@comAcast.net> writes

phdscholar80@yahoo.com wrote:

I am using the following code:

#include <vector>
#include <algorithm>

class A
{
};

bool comparator( A * b, const char * a )
{
// return appropriate true/false value
}

int main( int argc, char * argv[] )
{
std::vector< A * > vt;
const char * p = "a";

std::lower_bound( vt.begin(), vt.end(), p, comparator );

return 0;
}

Note that the second argument of the comparator function is the same
as 'p', the 'object' that is being provided. Is this a standard
compliant technique? If not, shouldn't the standard allow this? It
comes in useful in a LOT of situations. (Incase you are wondering,
this worked perfectly on Visual Studio 2003 but doesn't work on Visual
Studio 2005 with SP1).


I couldn't find any direct set of requirements for the 'Compare' argument
of 'lower_bound' template except that the container "should be partitioned
with respect to 'comp(e, value)'", where 'comp' is your 'comparator'.
Whether this requires the 'comparator' to be callable with 'a' and 'b'
reversed (that's what Visual C++ requires, and that's why it fails) is
open to interpretation.

I've tripped over this one as well.
IIRC the reason for VS2005 behaving this way is that (in debug mode) the
library does some run-time tests that the comparison really is a strict
weak ordering, by verifying that comp(a, b) and comp(b, a) are not both
true.
--
Richard Herring
.
User: ""

Title: Re: Signature of the predicate in std::lower_bound 25 Aug 2007 01:08:16 AM
On Aug 24, 5:51 pm, Richard Herring <junk@[127.0.0.1]> wrote:

In message <famirb$bs...@news.datemas.de>, Victor Bazarov
<v.Abaza...@comAcast.net> writes



phdschola...@yahoo.com wrote:

I am using the following code:


#include <vector>
#include <algorithm>


class A
{
};


bool comparator( A * b, const char * a )
{
// return appropriate true/false value
}


int main( int argc, char * argv[] )
{
std::vector< A * > vt;
const char * p = "a";


std::lower_bound( vt.begin(), vt.end(), p, comparator );


return 0;
}


Note that the second argument of the comparator function is the same
as 'p', the 'object' that is being provided. Is this a standard
compliant technique? If not, shouldn't the standard allow this? It
comes in useful in a LOT of situations. (Incase you are wondering,
this worked perfectly on Visual Studio 2003 but doesn't work on Visual
Studio 2005 with SP1).


I couldn't find any direct set of requirements for the 'Compare' argument
of 'lower_bound' template except that the container "should be partitioned
with respect to 'comp(e, value)'", where 'comp' is your 'comparator'.
Whether this requires the 'comparator' to be callable with 'a' and 'b'
reversed (that's what Visual C++ requires, and that's why it fails) is
open to interpretation.


I've tripped over this one as well.

IIRC the reason for VS2005 behaving this way is that (in debug mode) the
library does some run-time tests that the comparison really is a strict
weak ordering, by verifying that comp(a, b) and comp(b, a) are not both
true.

--
Richard Herring- Hide quoted text -

- Show quoted text -

Just discovered a way around so posting it for everyone's benefit. I
don't think this is something new, people probably already knew this.
I ought to have thought of this solution earlier. Anyways, the
solution is to create a class:
class Comparator
{
public:
bool operator( const char * a, A * b ) { // return a < b }
bool operator( A * a, const char * b ) { // return a < b }
};
and then
Comparator comp;
std::lower_bound( vt.begin(), vt.end(), "kdkdj", comp );
and TADA. Hope this helps.
.
User: "Victor Bazarov"

Title: Re: Signature of the predicate in std::lower_bound 25 Aug 2007 09:13:09 PM
wrote:

[..]
Just discovered a way around so posting it for everyone's benefit. I
don't think this is something new, people probably already knew this.
I ought to have thought of this solution earlier. Anyways, the
solution is to create a class:

class Comparator
{
public:

bool operator( const char * a, A * b ) { // return a < b }
bool operator( A * a, const char * b ) { // return a < b }

Just to note that this is not compilable code, it's some kind of
pseudo-code, I am guessing. Should probably be
bool operator()(const char*, A*) const; // ..
bool operator()(A*, const char*) const; // ..

};

and then

Comparator comp;
std::lower_bound( vt.begin(), vt.end(), "kdkdj", comp );

and TADA. Hope this helps.

Good idea, BTW.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER