new question: vector interator crashes

Started by
4 comments, last by Stoffel 19 years, 7 months ago
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]
Advertisement
you don't need bind2nd here.
Just do 'game' be member of UpdateMonster


struct UpdateMonster{ CSomething &game; UpdateMonster(CSomething & g) : game(g){}       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;        }     }; monsters.erase(                            std::remove_if(                                             monsters.begin(), monsters.end(),                                            UpdateMonster(local_game)                                          ),                            monsters.end()                        );
oh! Thank you, that works great! :)
Hi again. My game crashes if I loop with an interator from the vector.end() to the vector.begin()!

Here is the code that works:
std::vector<  boost::shared_ptr<str_map> >::const_iterator str_itr;//...for(  str_itr = input_list.begin(); str_itr != input_list.end(); ++str_itr)       {           b_font->draw( screen, 13, str_list_y_counter,  (*str_itr)->getBoth().c_str() );           str_list_y_counter+=20;       } 



And this one doesn't work! (crash)
std::vector<  boost::shared_ptr<str_map> >::const_iterator str_itr;//...for(  str_itr = input_list.end(); str_itr != input_list.begin(); --str_itr)       {           b_font->draw( screen, 13, str_list_y_counter,  (*str_itr)->getBoth().c_str() );           str_list_y_counter+=20;       } 


Why? :/? Thank you! :)
I believe you have to use a reverse iterator, as IIRC, vector.end() points to one past the end.

This MSDN article will help.

EDIT: Link
Quote:Original post by desertcube
I believe you have to use a reverse iterator, as IIRC, vector.end() points to one past the end.

This MSDN article will help.



Exactly. You have to use a reverse_iterator, and iterate from rbegin (which points to the end) to rend (which points just before the beginning).

This topic is closed to new replies.

Advertisement