Passing a class method as opposed to a function in std :: sort

Inside the class, I'm trying to sort a vector by passing a method of the same class. But it gives errors at compile time. Can anyone tell what the problem is? Thank!

it gives the following error: argument of type bool (Sorter::)(D&, D&)' does not matchbool (Sorter :: *) (D &, D &) '

I also tried using sortBynumber(D const& d1, D const& d2)

#include<vector>
#include<stdio.h>
#include<iostream>
#include<algorithm>

class D {
      public:                   
             int getNumber();            
             D(int val);
             ~D(){};
      private:
              int num;
};

D::D(int val){
         num = val;
         };

int D::getNumber(){
    return num;
};


class Sorter {
      public:                   
             void doSorting();  
             bool sortByNumber(D& d1, D& d2);
             std::vector<D> vec_D;          
             Sorter();
             ~Sorter(){};
      private:
              int num;
};

Sorter::Sorter(){                 
        int i;
        for ( i = 0; i < 10; i++){
            vec_D.push_back(D(i));
           }
         };

bool Sorter::sortByNumber(D& d1, D& d2){
     return d1.getNumber() < d2.getNumber();
     };

void Sorter::doSorting(){
     std::sort(vec_D.begin(), vec_D.end(), this->sortByNumber);
     };




int main(){    
    Sorter s;
    s.doSorting();

    std::cout << "\nPress RETURN to continue...";
    std::cin.get();

    return 0;
}
+3
source share
4 answers

Make it Sorter::sortByNumberstatic. Since it does not refer to any members of the object, you no longer need to change anything.

class Sorter {
public:                   
    static bool sortByNumber(const D& d1, const D& d2);
    ...
};

// Note out-of-class definition does not repeat static
bool Sorter::sortByNumber(const D& d1, const D& d2)
{
    ...
}

You should also use const references, as sortByNumberyou must not modify objects.

+5
source

- , operator< :

class D { 
    int val;
public:
    D(int init) : val(init) {}
    bool operator<(D const &other) { return val < other.val; }
};

class sorter { 
    std::vector<D> vec_D;
public:
    void doSorting() { std::sort(vec_d.begin(), vec_D.end()); }
};

sorter D , (, , - ).

, sorter . SortByNumber , , :

class D { 
    std::string name;
    int height;
    int weight;
// ...
};

D , . D, , , :

namespace D { 
class D { 
    std::string name;
    int height;
    int weight;
public:
    friend class byWeight;
    friend class byHeight;
    friend class byName;
    // ...
};

struct byWeight { 
   bool operator()(D const &a, D const &b) { 
       return a.weight < b.weight;
   }
};

struct byHeight {
    bool operator()(D const &a, D const &b) { 
        return a.height < b.height;
    }
};

struct byName { 
    bool operator()(D const &a, D const &b) { 
        return a.name < b.name;
    }
};
}

:

std::vector<D::D> vec_D;

// sort by height:
std::sort(vec_D.begin(), vec_D.end(), D::byHeight());

// sort by weight:
std::sort(vec_D.begin(), vec_D.end(), D::byWeight());

// sort by name:
std::sort(vec_D.begin(), vec_D.end(), D::byName());

, . . , . , ( ).

( ) , ( ).

+3

sortByNumber() -. , , ( ). :

struct sortByNumber {
    bool operator()(const D& d1, const D& d2) const {
        return d1.getNumber() < d2.getNumber();
    }
};

. , , , . :

std::sort(vec_D.begin(), vec_D.end(), sortByNumber());

, , , boost::bind():

std::sort(vec_D.begin(), vec_D.end(),
          boost::bind(&Sorter::sortByNumber, this, _1, _2));

, #include <boost/bind.hpp>.

+2

sortByNumber - Sorter. , , . , const , . :

First change the function int getNumber()to const asint getNumber() const;

Then write a free function sortByNumber, again using the parameters using the const link.  bool sortByNumber(const D& d1, const D& d2);

You can call sortas: std::sort(vec_D.begin(), vec_D.end(), sortByNumber);

+1
source

Source: https://habr.com/ru/post/1744882/


All Articles