I'm trying to figure out how to use the max_element algorithm on a vector of structs, rather than POD's like int etc.

This is my struct:

struct NameComponents
      {
         std::string sValue;
         std::string sDescriptor;
         int         nOrder;
         int         nStatus;
         
         // Construtor.
         NameComponents( std::string inValue,
                         std::string inDescriptor,
                         int         inOrder ) : sValue( inValue ),
                                                 sDescriptor( inDescriptor ),
                                                 nOrder( inOrder ),
                                                 nStatus( -1 ) { }
      
      };
      
      typedef std::vector < NameComponents >         NameComponentContainer;
      typedef NameComponentContainer::iterator       NcIter;
      typedef NameComponentContainer::const_iterator NcIterC;

      NameComponentContainer m_nameComponents;

What I'd like to do is locate the max_element for member nOrder, any help appreciated.

max_element has 2 forums, one compares elements automatically using the < the other accepts a comparison function as well as the 2 iterators.

Therefore either you need to put an operator< method into your structure or you need to provide a comparison function and use the second forum.

In this case I would opt for using a comparison function because operator< has no proper symantic meaning for your structure.

See max_element reference

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.