c++ - Heap sorting vector of Clients, passing iterators incorrectly? -


i working on program creates vector of 4 clients, heap sorts them using comparator class (sorting account balance essentially). after i'm printing out ordered account numbers of each client in vector. however, don't think have implemented correctly. firstly, when call stl heap sort on vector, understand pass comparator third argument. under impression needed call object of accountorder comparator class, gave me large number of errors. if call on class , not instance of class, doesn't give me many errors. correct way this?

int main() {     accountorder f;      vector<client> list;      client client1(345.88, "charlie");     client client2(249.12, "rashesh");     client client3(442.90, "michael");     client client4(543.74, "mary");      list.push_back(client1);     list.push_back(client2);     list.push_back(client3);     list.push_back(client4);      make_heap(list.begin(), list.end(), f);     sort_heap(list.begin(), list.end(), f);      for(int = 0; < list.size(); i++)          cout << list[i].getaccountbalance();      system("pause");     return 0; } 

comparator class:

class accountorder  {     public:     bool operator()(client * a, client * b)      {         return a->getaccountbalance() > b->getaccountbalance();     }; }; 

the comparator has been implemented in code above main() function. secondly, function need pass comparator to? (here i've passed both, i'm not sure yet).

make comparator & instead of *

class accountorder  {     public:     bool operator()(const client &a, const client &b) const     {         return a.getaccountbalance() > b.getaccountbalance();     }; }; 

 

permake_heap , sort_heap:

the signature of comparison function should equivalent following:

bool cmp(const type1 &a, const type2 &b);

the signature not need have const &, function must not modify objects passed it.


Comments