Problems with iterators - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: Problems with iterators

  1. #16
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,370

    Re: Problems with iterators

    Quote Originally Posted by Paul McKenzie View Post
    At one of my jobs, the programmers there were doing basically the same thing as the OP. They could never figure out how to erase from a vector while looping -- you should have seen the creative ways that were done, all would eventually fail. Things like jumping over invalidated iterators, reseating the iterator, all sorts of things that made it a nightmare to maintain and work consistently.

    I introduced them to std:artition() and erase(), and all of those problems that were plaguing that part of the code (for months) became resolved in less than 5 minutes.
    ...and the moral of the story is to know your STL algorithms!

    PS I wish that c++ tutors and books would remember the STL and give all of it the coverage it deserves - not just vectors, maps and i/o streams if you're lucky.
    Last edited by 2kaud; January 21st, 2014 at 11:59 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Problems with iterators

    Quote Originally Posted by Droopy View Post
    Still not working. Now I get the following error: Error 1 error C3867: 'Sname::Engine::hasCollided': function call missing argument list; use '&Sname::Engine::hasCollided' to create a pointer to member
    The function object as I've written needs to be a global or static function, not a non-static member function.

    There is a way to get a non-static member function as part of the call, and that is to use std::mem_fun_ref to convert a pointer-to-member into a function object. It's a little advanced, but I will show you here:
    Code:
    if (innerHit)
    {
       sprites.erase(remove_if(sprites.begin(), sprites.end(), std::mem_fun_ref(&Enemy::hasCollided)), sprites.end());
    }
    This now calls your member function Enemy::hasCollided for each one of the sprites. If you still want the function to be more "local" and were not using Visual Studio 2013, you could do this using this method:
    Code:
    if (innerHit)
    {
       struct HasCollidedImpl { static bool HasCollided(Sprite* s) { return s->getCollided(); } };
       sprites.erase(remove_if(sprites.begin(), sprites.end(), &HasCollidedImpl::HasCollided), sprites.end());
    }
    The above creates a local struct with a static function that test for collision. You then feed the erase() function with the address of this function.

    Since you are using Visual Studio 2013, C++ now supports lambda functions -- these are local functions that you plug directly into the algorithm call (similar to example above). The difference is that C++ officially supports this. I haven't used the lambda functions, but I believe this should work:
    Code:
    if (innerHit)
    {
       sprites.erase(remove_if(sprites.begin(), sprites.end(), [](Sprite *s) { return s->getCollided(); }), sprites.end());
    }
    Note that the third argument to remove_if is an entire function body and not a pointer to a function or function object. The syntax is weird if you don't know lambda's, as they can have various options and can get advanced.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,370

    Re: Problems with iterators

    and that is to use std::mem_fun_ref to convert a pointer-to-member into a function object.
    mem_fun_ref() is part of <functional> See
    http://www.cplusplus.com/reference/f...l/mem_fun_ref/
    http://msdn.microsoft.com/en-us/library/741a5f2d.aspx

    The syntax is weird if you don't know lambda's, as they can have various options and can get advanced.
    That's an understatement!
    Last edited by 2kaud; January 21st, 2014 at 02:24 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #19
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Problems with iterators

    Quote Originally Posted by 2kaud View Post
    ...and the moral of the story is to know your STL algorithms!
    Since this is the Visual C++ forum, I would also like to mention that partition(), remove_if(), sort(), random_shuffle(), and most of the algorithms works for the MFC CArray class and all of the CxxxxArray container classes.

    A lot of people think STL is just about containers, not realizing there are a whole lot of algorithm functions that work on sequences of data, regardless of how the sequence is stored (array, vector, CArray, etc.).

    Regards,

    Paul McKenzie

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center