c++ - Sorting vector of instances -


i taking coding class @ university , have specific requirements homework assignments.

for week have class called npt represents nobel prize winner. class contains, amongst other things, name, year of prize , field of winner.

now should make class, nobelpreise, contains container instances of said nobel prize winner class. supposed sort elements of container year of nobel prize.

i wasn't able use std::sort function custom comparator correctly. code looks like:

class nobelpreise { private:  int numb;  vector<npt> xx; public:  nobelpreise(){numb=0;}  void add(npt &n1){xx.push_back(n1);numb++;return;}  npt get_nobel(int i) {return xx[i];}  vector<npt> get_xx() {return xx;}  int get_numb(){return numb;}  ~nobelpreise(){}  bool mycomp(npt n1, npt n2) {return (n1.get_jverl()<n2.get_jverl());}    }; 

the method get_jverl() comes npt class , returns year.

now sort function gives error saying that:

sort(npl.get_xx().begin(), npl.get_xx().end(), npl.mycomp) 

requires 2 arguments. shouldn’t provided sort function? have tried overload < operator not seem work either.

edit1: added end() , removed () npl.mycomp
edit2: required make comparator function member of class

make method mycomp static, , write method sort job.

class nobelpreise { private:     int numb;     vector<npt> xx; public:     nobelpreise(){numb=0;}     ~nobelpreise(){}     vector<npt>& get_xx() {return xx;}      static bool mycomp( const npt &n1, const npt &n2 ) { return n1.get_jverl() < n2.get_jverl(); }   //^^^^^^      void sort() { std::sort( xx.begin(), xx.end(), &mycomp ); } }; 

the method npt::get_jverl has const

returntype get_jverl() const { return ...; }

if sorting outside class note have return reference vector in method: vector<npt>& get_xx() {return xx;}

sort(npl.get_xx().begin(), npl.get_xx().end(), &nobelpreise::mycomp) 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -