代码之家  ›  专栏  ›  技术社区  ›  nburk

如何在C++中使用带有自定义排序成员函数的sort()?

  •  3
  • nburk  · 技术社区  · 9 年前

    关于将比较函数传递给 sort() .

    我想做的是定义 排序() 函数,该函数在计算时考虑了要在其中进行排序的类的成员变量。

    基本上,我的代码如下(简化为仅显示相关部分):

    映射技术.h

    struct MappingTechnique {
        vector<int> usedIndexCount; 
    };
    
    struct SimpleGreedyMappingTechnique : MappingTechnique {
        bool sortByWeights(int index1, int index2);
    };
    

    映射技术.m

    bool SimpleGreedyMappingTechnique::sortByWeights(int index1, int index2) {
        return usedIndexCount[index1] > usedIndexCount[index2];
    }
    
    void SimpleGreedyMappingTechnique::processFrame(Frame frame) {
        vector<int> payloadIndices = <generate the vector>
    
        // sort the payload indices according to their current usedIndexCount
        sort(payloadIndices.begin(), payloadIndices.end(), sortByWeights);
    }
    

    此代码无法编译,它会出现以下错误:

     error: reference to non-static member function must be called
    

    并指向 sortByWeights .

    甚至可以使用类的成员函数进行排序吗?如果是,我该如何实施?

    2 回复  |  直到 9 年前
        1
  •  10
  •   Romano Zumbé Ajay Jain    7 年前

    是的,但总的来说,我鼓励只使用适当的函子或lambda:

    使用lambda:

    std::sort(payloadIndices.begin(), payloadIndices.end(), [this](int a, int b){
        return this->sortByWeights(a, b);
    });
    

    或者使用 std::mem_fn :

    auto sorter = std::bind(std::mem_fn(SimpleGreedyMappingTechnique::sortByWeights), this);
    std::sort(payloadIndices.begin(), payloadIndices.end(), sorter);
    

    或者使用函子:

    namespace{
    struct indicies_less_than
    {
        const SimpleGreedyMappingTechnique & mapping_tech;
        indicies_less_than(const SimpleGreedyMappingTechnique & mapping_tech)
           :mapping_tech(mapping_tech){}
    
        bool operator()(int a, int b)
        {
           return mapping_tech.sortByWeights(a, b);
        }
    };
    }
    
    std::sort(payloadIndices.begin(), payloadIndices.end(), indicies_less_than(*this));
    

    注:

    如果排序的类型比 int 你肯定想让他们路过 const& 防止复制

        2
  •  2
  •   rcgldr    9 年前

    如Mgetz所述,可以使用函子。函子示例:

    #include <algorithm>
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <vector>
    
    #define asize 16
    
    class example{
    public:
        unsigned int a[asize];      // array
        std::vector<size_t> v;      // indices to array
    
    example(void)
    {
        v.resize(asize);
        for(size_t i = 0; i < asize; i++){
            a[i] = rand()%10;
            v[i] = i;
        }
    }
    
    void displayarray(void)
    {
        for(size_t i = 0; i < asize; i++)
            std::cout << std::setw(3) << a[v[i]];
        std::cout << std::endl;
    }
    
    class lessthan                  // lessthan functor for std::sort
    {
    public:
    const example &x;
        lessthan(const example &e ) : x(e) { }
        bool operator()(const size_t & i0, const size_t & i1)
        {
            return x.a[i0] < x.a[i1];
        }
    };
    
    void sortarray(void)
    {
        std::sort(v.begin(), v.end(), lessthan(*this));
    }
    };
    
    int main()
    {
    example x;
    
        x.displayarray();
        x.sortarray();
        x.displayarray();
        return 0;
    }