Jump to content
  • Advertisement
Sign in to follow this  
persil

Functors, STL algorithms, how to?

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this question, how do you organize your source/includes when using STL algorithm and making functors. I mean, when I start wanting to use this, I get small function objects that lay here and there, how should I organize this??? If I write them in the header file, it's exposed, and most of the time, it's for internal procedures that I'd neeed them. So I put them in the source file, but then, it's mixed up with the implementation, so I end up not using them anyway in the end. What are your methods? Do you even use functors?

Share this post


Link to post
Share on other sites
Advertisement
for short, one shot code i would either use a local class (rarely) or lambda expression (mostly) [grin] using boost's lambda library.

Lets you do things like:


for_each(a.begin(), a.end(), std::cout << _1 << ' ');


and much, much more.

You might be interested in FC++ library, lets you do functional programming in C++ aswell.

Share this post


Link to post
Share on other sites
Thanks, I'm looking into this right now, however, it's qiute disconcerting... I'm not used to such a weird syntax.

What I'm trying to accomplish is to use find_if or for_each with the second element of a pair within maps.

Like:

typedef std::map< unsigned int, boost::shared_ptr< entity > > map_type;

for_each( m_map.begin(), m_map.end(), bind2nd( mem_fun(set_visible), true );

for example, kind of, I don't know how to do that with lambda.

Share this post


Link to post
Share on other sites
while i do like for_each and boost, sometimes plain old c++ works just fine:

for( map_type::iterator i = m_map.begin(); i != m_map.end(); ++i ) i->second->set_visible( true );

Share this post


Link to post
Share on other sites
for functors that are local to a translation unit I prefer putting them in an annonymous namespace at the top of the implementation file.

I (try to) only ever put stuff in header files that somehow contributes to the interface of the class or module.

Share this post


Link to post
Share on other sites
Quote:
Original post by persil
Thanks, I'm looking into this right now, however, it's qiute disconcerting... I'm not used to such a weird syntax.

What I'm trying to accomplish is to use find_if or for_each with the second element of a pair within maps.

Like:

typedef std::map< unsigned int, boost::shared_ptr< entity > > map_type;

for_each( m_map.begin(), m_map.end(), bind2nd( mem_fun(set_visible), true );

for example, kind of, I don't know how to do that with lambda.



for_each( m_map.begin(), m_map.end(), bind(&entity::set_visible, _1, true));

...but that only marginally looks better than your method which only uses the STD library.

Share this post


Link to post
Share on other sites
mgarriss: you're right, for now that's how I have it implemented, I was just wondering how it could be done, as I want to try getting STL and functionnal programming into my head :)

I could even do this, as the expression is very simple.

for( map_type::iterator i = m_map.begin(); i != m_map.end(); i++->second->set_visible( true ) );

DigitalDelusion: Seems like a good idea, but exactly what's an anonymous namespace?

mfawcett: my example doesn't work :) Because the iterator is of the map type, it is a pair, so I'm searching for a way to access the first or second item of the pair within an STL expression. Or Lambda.

Share this post


Link to post
Share on other sites
Oh, sorry for the hasty response then. You should look into select1st and select2nd if that is the case, I think they are SGI extensions. There also used to be a boost library implementation called over_first and over_second.

Share this post


Link to post
Share on other sites
Quote:
Original post by persil
DigitalDelusion: Seems like a good idea, but exactly what's an anonymous namespace?



namespace
{
//this is an anonymous namespace and whatever you put inhere will
//have internal linkage this is the prefered way to achive what you
//in C would get by declaring a function 'static'
};



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!