Hello guys! I don't understand how I can use bind2n! Snk_kid showed it me once with an example.. but I don't understand it ( see last sourcebox for it)
I have this functor:
struct UpdateMonster
{
bool operator()(CMonster* monster)
{
if(monster->think
(
game.getScreen(),
game.getScroll_x(), game.getScroll_y(),
game.getPlayer_x(), game.getPlayer_y(),
game.getPlayer_w(), game.getPlayer_h()
) == -1 )
{
delete monster;
return true;
}
return false;
}
};
that I call this way:
monsters.erase(
std::remove_if(
monsters.begin(), monsters.end(),
UpdateMonster()
),
monsters.end()
);
// monsters is a vector
But as you see game is global! I don't want that game is global!
Please show me how I can use bind2n ( or something else ) with my MonsterUpdate code, so that I don't have to use a global object.
Thank you so much!!
See you.. rak
bind2n example I don't understand:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
#include <cstdlib>
#include <ctime>
template< typename T >
struct less_than : public std::binder2nd< std::less< T > > {
explicit less_than(const T& a)
: std::binder2nd< std::less<T> >(std::less<T>(), a) {}
};
template <unsigned low, unsigned high>
struct random
{
unsigned operator()(void){ return (rand() % (high-low)) + low;}
};
int main(void)
{
srand( time(0));
std::vector<int> v;
std::generate_n( std::back_inserter(v), 10, random<0, 10>());
v.erase( std::remove_if( v.begin(), v.end(), less_than<int>(5)), v.end());
std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cin.get();
}
[Edited by - rakoon2 on August 26, 2004 5:05:22 PM]