Search code examples
c++vectormaxuser-defined-types

Getting the max element in a vector of objects from a custom class


Consider the following blocks of code:

template<typename Prefs> class SocialPrefNode{

public:

// Constructors & Destructor
SocialPrefNode( );
SocialPrefNode( char self, std::vector<SocialPrefNode*> pref );
SocialPrefNode( const SocialPrefNode& copy );

~SocialPrefNode( );

// Setters
void set_id( char self );

void set_pref( std::vector<SocialPrefNode*> prefs );
void set_pref( SocialPrefNode& prefs );

void set_worse( std::vector<SocialPrefNode*> wrs );
void set_worse( SocialPrefNode& wrs );

void set_indiff( std::vector<SocialPrefNode*> indiff );
void set_indiff( SocialPrefNode& indiff );

// Getters
char get_id( ){ return id; }

std::vector<SocialPrefNode*> get_preferences( ){ return preferences; }
std::vector<SocialPrefNode*> get_worse( ){ return worsethan; }
std::vector<SocialPrefNode*> get_indiff( ){ return indifference; }

// Operators
SocialPrefNode& operator=( const SocialPrefNode& copy );

private:

char id{ };

std::vector<SocialPrefNode*> preferences{ };
std::vector<SocialPrefNode*> worsethan{ };
std::vector<SocialPrefNode*> indifference{ };
};

,and

std::vector<SocialPrefNode<char>> graph

, where the latter is made by pointing its elements to each other.

How can I get the maximum element, in terms of the size of the preferences vector's size, from graph?

I.e., I want to select elements from graph accordingly to preferences' size, in descending order.

I have considered using std::max_element( ), but it does not seem to apply to the aforementioned code, since I am using a vector of objects from a custom class where there are no properties to directly inform the compiler what is to be considered a greater or smaller element in the vector.

Thanks for the help.


Solution

  • You can use std::max_element with a custom comparison function that compares the two objects however you'd like. See overload #3.

    #include <vector>
    #include <algorithm>
    #include <cassert>
    
    class Node
    {
    public:
        Node(const std::vector<int>& prefs) : prefs_(prefs) {}
    
        size_t numPrefs() const { return prefs_.size();}
    
        bool operator==(const Node& rhs) const { return prefs_ == rhs.prefs_;}
    
    private:
        std::vector<int> prefs_;
    };
    
    
    
    int main(int argc, char** argv)
    {
        Node one(std::vector<int>{1});
        Node two(std::vector<int>{1,2});
        Node three(std::vector<int>{1,2,3});
    
        std::vector<Node> nodes{one, two, three};
    
        auto comparison = [](const Node& a, const Node& b)
            {
                return a.numPrefs() < b.numPrefs();
            };
    
        auto max = std::max_element(nodes.begin(), nodes.end(), comparison);
    
        assert(*max == three);
        //assert(*max == two);  //Will fail
    }