Thread: Help with iterator algorihms

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    Help with iterator algorihms

    One problem i can't seem to find the solution to is that i can't seem to access a class from a container using an iterator and the algorihm functions. like if the container was like this:

    Code:
    vector<int> Vector;
    then getting the value from it would be easy:

    Code:
    (*i) = find(Vector.begin(),Vector.End(),40);
    but what if you had something like this:

    Code:
    vector<Entity*> Ent;
    
    (*i) = find(Ent.begin(),Ent.end(), ent);
    obviously that would produce an error and which instance of the class would i be getting from the iterator? 0? 40? 100? I can't figure it out how do do that with a container that has user defined types. Maybe I'm wrong, maybe there's a way i'm just not getting

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, what would the type of i have to be (a pointer to iterator???)?

    The second version would return an iterator to a pointer with the same address as ent. If you want to compare something else than just the pointer values, then you'd need to use find_if with a suitable predicate.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    30
    As far as find algorithm is concerned, I think as long as the user-defined type defines equality operator (operator ==), it will work.

Popular pages Recent additions subscribe to a feed