Confused about list "find" Technique

M

Mike Copeland

I am trying to develop code that will "find" if a value exists in an
STL list structure. Perhaps I'm being too clever in my attempt to use
code snippets I've found (and maybe I should be doing this search in a
more brute-force manner with an iterator loop), but what I have is the
code below. It doesn't compile (many errors), so I know I'm
misunderstanding something fundamental.
I probably need to use find_if with this (mess), but I haven't gotten
far enough to see what will actually work. Note that I'm using a list
because there's no unique key and "body" values may be duplicated.
Please advise. TIA

class DBE_DATA
{
public
struct DBEBUILD
{
int bibNumber;
char source;
string body; // this is the data I want to match
} dbeWork;
list<DBEBUILD> zeroBibList;
list<DBEBUILD>::iterator zblIter;
};
struct sameName: public binary_function<DBE_DATA, string, bool>
{
bool operator()(const DBE_DATA *oldName, string newName) const
{
return oldName == newName;
}
};
 
I

Ian Collins

I am trying to develop code that will "find" if a value exists in an
STL list structure. Perhaps I'm being too clever in my attempt to use
code snippets I've found (and maybe I should be doing this search in a
more brute-force manner with an iterator loop), but what I have is the
code below. It doesn't compile (many errors), so I know I'm
misunderstanding something fundamental.
I probably need to use find_if with this (mess), but I haven't gotten
far enough to see what will actually work. Note that I'm using a list
because there's no unique key and "body" values may be duplicated.
Please advise. TIA

class DBE_DATA

First a point of style: avoid all caps for anything other than macros,
it looks ugly and distracts readers.
{
public
struct DBEBUILD
{
int bibNumber;
char source;
string body; // this is the data I want to match
} dbeWork;
list<DBEBUILD> zeroBibList;
list<DBEBUILD>::iterator zblIter;
};

So you have a list of structures to search. You have two options, ether
add an operator== to DBEBUILD (after changing its name!) or provide a
function object to do the comparison:

struct sameName
{
const std::string match;

sameName( const std::string& ref ) : match(ref) {}

bool operator()( const DBEBUILD& other )
{
return match == other.body;
}
};

Which you can use with std::find_if.

The second approach is normally used if it is inappropriate or
impossible to add an operator== to the stored type.
 
R

red floyd

I am trying to develop code that will "find" if a value exists in an
STL list structure. Perhaps I'm being too clever in my attempt to use
code snippets I've found (and maybe I should be doing this search in a
more brute-force manner with an iterator loop), but what I have is the
code below. It doesn't compile (many errors), so I know I'm
misunderstanding something fundamental.
I probably need to use find_if with this (mess), but I haven't gotten
far enough to see what will actually work. Note that I'm using a list
because there's no unique key and "body" values may be duplicated.
Please advise. TIA

[redacted]

So you have a list of structures to search. You have two options, ether
add an operator== to DBEBUILD (after changing its name!) or provide a
function object to do the comparison:

[redacted]

The second approach is normally used if it is inappropriate or
impossible to add an operator== to the stored type.

Third alternative: add operator<(), or specialize std::less<>() and use
std::find().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top