0

This is a continuation on my previous question here regarding retrieving and editing private members of objects in a vector.

I have a vector full of objects that have private members that I need to access. I have created accessor functions for returning each private member and am now looking for the best way to iterate through the vector of objects, returning a specific private member from it and comparing it to a given variable until a match is found.

I have considered using find_if but have had no luck with using a member functions return value, for each object in the vector, as criteria.

Neither have I been able to use for_each to do the job.

The reason I am creating an additional question is that I've been dwelling for this longer than I should and it's getting to become quite urgent that I find a solution to the problem. Any nudge in the right direction would be greatly appreciated!

Community
  • 1
  • 1
vaent
  • 11
  • 3

1 Answers1

2

Create a functor:

struct CompareTo{
    CompareTo(const AnotherObject& aValue) : theValue(aValue){}

    bool operator()(const Object& anObject) const{
        return anObject.getMemberVar() == theValue;
    }

    const AnotherObject& theValue;
};

that can be put into the find_if.

wheaties
  • 35,646
  • 15
  • 94
  • 131