473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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).

Aug 24 '07 #1
1 3594
ph**********@yahoo.com 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
Aug 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
============================= Windows 2000 MinGW 2.0.0.-2 GNU gcc/g++ version 3.2 ============================= I have some question concerning the lower_bound algorithm. ========= C++ code...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
3
by: Allerdyce.John | last post by:
On page 181 of Effective STL, it said 'It 's knowning when equal_range is a better way to search than lower_bound, knowing when lower bound is preferable to find..." My question is understand...
3
by: Diego Martins | last post by:
Andrew Koenig wrote:
8
by: brekehan | last post by:
If I have a class MyClass { ...bunch o data and methods int x; }; and a stl container of MyClass objects Is there a way to use std::find to get all the elements whose member x
0
by: mathieu | last post by:
hi there, I do not understand why the following piece of code is not working for me. I have a set of cstrings, where I am trying to find one 'UT'. If I search from "AE" to "OB or OW" everything...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I...
4
by: RiderOfGiraffes | last post by:
Hi, I'm trying to compile (someone else's) code on SuSE 11.0. It worked fine on SuSE 10.3, but now it's telling me that lower_bound is not a member of std. I'm assuming something has changed...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.