Sign in to follow this  

new question: vector interator crashes

This topic is 4854 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

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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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()
);




Share this post


Link to post
Share on other sites
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! :)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 4854 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this