functors and stl find_if

M

Michael

Hi,
I'm having a few problems getting the following find_if to work

class Entity
{

};

class factory
{
public:
string GetType();
Entity* CreateEntity();

};

class TheFactories
{
vector<factory*> factories;
public:
Entity* CreateEntity( const string& type)
{
vector<factory*>::iterator fact= find_if(

factories.begin(),

factories.end(),
/* What goes
here! */
);
if( fact != vector<factory*> ) return
(*fact)->CreateEntity();
assert(0);

}

};

now I know I could unroll the find_if but I'm trying to get the hang of
function objects and the like.

If i have vector<string>, i can use equal_to<string>() but I don't
understand where the arguements are supposed to go.
I'd have thought the syntax would be:
equal_to<string>(str1,str2)
not bind1st( equal_to<string>(), str2 )!

Ahhhhhh!

Thanks Mike
 
M

Matthias

Michael said:
/* What goes
here! */

A functor or a function returning bool! Not a conditional statement.
If i have vector<string>, i can use equal_to<string>() but I don't
understand where the arguements are supposed to go.

You have to use an adaptor like bind2nd:

std::vector<string> coll;
std::string string_which_equals_hello = find_if( coll.begin(),
coll.end(), std::bind2nd( std::equal_to<string>(), "hello" ) );
 
M

Matthias

Matthias said:
A functor or a function returning bool! Not a conditional statement.



You have to use an adaptor like bind2nd:

std::vector<string> coll;
std::string string_which_equals_hello = find_if( coll.begin(),
coll.end(), std::bind2nd( std::equal_to<string>(), "hello" ) );

Of course find_if returns an iterator, not a string :)
Correct that to:

vector<string>::iterator pos = find_if(...);

Sorry.
 
S

SnaiL

Michael said:
Hi,
I'm having a few problems getting the following find_if to work

class Entity
{

};

class factory
{
public:
string GetType();
Entity* CreateEntity();

};

class TheFactories
{
vector<factory*> factories;
public:
Entity* CreateEntity( const string& type)
{
vector<factory*>::iterator fact= find_if(

Use const_iterator instead of iterator.
factories.begin(),

factories.end(),
/* What goes
here! */

What goes here? Prefferable a function object.
To find factory you wanna find, only one way is to write a simple
functor to compare values, for example (it is not a best example, but
many compilers should support it):

template <typename PType, typename ArgType>
class is_needed_factory : unary_function<PType, bool>
{ public:
is_needed_factory(const ArgType & arg) : _name(arg) { }
~is_needed_factory() { }

bool operator()(const PType p) const
{
return _name == p->GetType();
}

private:
ArgType _name;
};

So, you will write something like this:

vector<factory*>::const_iterator fact =
find_if(factories.begin(), factories.end(),
);
if( fact != vector<factory*> ) return
(*fact)->CreateEntity();

Oops, fact is not a pointer to the vector of factory* pointers, fact is
pointer to the factory object, this compare statement is invalid.
Moreone, what value will be retuned if else?

Some better code:

if (fact != factories.end()) return (*fact)->CreateEntity();
else return 0;
 
S

SnaiL

#include <vector>
#include <string>
#include <functional>
#include <algorithm>

using namespace std;

class Entity
{
};

class factory
{
public:
string GetType() const;
Entity* CreateEntity();

};

template <typename PType, typename ArgType>
class is_needed_factory : unary_function<PType, bool>
{ public:
is_needed_factory(const ArgType & arg) : _name(arg) { }
~is_needed_factory() { }

bool operator()(const PType p) const
{
return _name == p->GetType();
}

private:
ArgType _name;
};

class TheFactories
{
vector<factory*> factories;

public:
Entity* CreateEntity(const string & type)
{
vector<factory*>::const_iterator fact =
find_if(factories.begin(), factories.end(),
is_needed_factory<factory*, string>(type));

if (fact != factories.end()) return (*fact)->CreateEntity();
else return 0;
}
};
 

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

Similar Threads

STL map find_if 5
Functors 2
STL bind1st question 5
OpenMP and functors 1
find_if algorithm on multimaps 3
find_if and mem_fun not getting expected results 1
using find_if/binary_function 6
list find_if usage 3

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top