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 4 7101
Michael wrote: /* 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" ) );
--
Regards,
Matthias
Matthias wrote: Michael wrote:
/* 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" ) );
Of course find_if returns an iterator, not a string :)
Correct that to:
vector<string>::iterator pos = find_if(...);
Sorry.
--
Regards,
Matthias
Michael wrote: 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(),
is_needed_factory<factory*, string>(type));
); 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;
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
#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;
}
}; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mikets |
last post by:
Hi there,
I found out that very often I use the following construct:
for (it = myvec.begin(); it != myvec.end(); ++it) {
if (it->name() == "myname")
break;
}
Certainly, the container the...
|
by: John Black |
last post by:
Hi,
In using find_if() you need to name a comparision function which
normally is a static function, but sometimes it is really inconvinient
for this, let's take this example,
class MyClass{...
|
by: dave_if |
last post by:
I have a vector of TCard objects, I am sorting them based on the the
box field. That part works fine (finally!). I would then like to
random_shuffle all cards that are in a particular box, that is,...
|
by: ma740988 |
last post by:
Trying to get more acclimated with the use of function objects. As
part of my test, consider:
# include <vector>
# include <iostream>
# include <algorithm>
#include <stdexcept>
#include...
|
by: devel |
last post by:
Hello,
Could someone tell me why the find_if statement applied to my multimap
dictionnary is doesn't compile? Does this algorithm doesn't work on a
multimap?
I don't understand why the...
|
by: AlanJSmith |
last post by:
can some clever guru help please.
I am porting some C++ 6.0 projects to C++ VS2005 and find_if no longer exists in vector, i am not sure what to do. i am only at begginer level so please dont get...
|
by: tryptik |
last post by:
Hello all,
I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:
class Functor {
std::string...
|
by: Jon Slaughter |
last post by:
I'm trying to mess with functors and the way I want it to work is that when
I create a functor it will automatically add itself to an array.
The attached code demonstrates what I mean. The...
|
by: Christopher |
last post by:
I used to just use a plain old function pointer is a call to
std::sort.
My colleagues are telling me that I need to use a "functor". Ok, I
google and see that a functor is a class with a public...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |