简体   繁体   English

为什么当我将成员 function 传递给 sort() 时会抛出错误

[英]why it throws an error when I pass a member function to sort()

I want to sort a vector whose member is pair<int, int>.我想对成员为 pair<int, int> 的向量进行排序。 So I write a function to compare two pairs.所以我写了一个 function 来比较两对。 It works when the function is in the outer space.它在 function 在外太空时有效。 But it throws an error when I try to write it as a member function. So why the error is thrown?但是当我尝试将其写为成员 function 时它会抛出错误。那么为什么会抛出错误呢?

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue> 


using std::pair;
using std::vector;
using std::sort;
using std::endl;
using std::cout;


// bool cmp(const pair<int, int>& p1, const pair<int, int>& p2)
// {
//     return p1.second < p2.second;
// }


class test
{
    public:
    bool cmp(const pair<int, int>& p1, const pair<int, int>& p2)
    {
        return p1.second < p2.second;
    }

    void my_sort(vector <pair<int, int>> &data)
    {
        sort(data.begin(), data.end(), cmp);
    }
};


int main()
{
    vector <pair<int, int>> data;
    for(int i=10; i>=0; i--)
    {
        data.push_back(pair<int, int>(10-i, i));
    }
    test a;
    a.my_sort(data);
    // sort(data.begin(), data.end(), cmp);
    for (auto &&i : data)
    {
        cout << i.first << '\t' << i.second << endl;
    }
    
    return 0;
}

So why the error is thrown?那么为什么会抛出错误呢?

Because cmp is a non-static memeber function and requires an object to be called on and so cannot be used as the comparison function object in std::sort .因为cmp是非静态成员function 并且需要调用 object ,所以不能用作std::sort中的比较 function object 。

To solve this you can either make cmp a static member function or use a lambda or implement cmp outside the class(at global namespace) as shown below:解决此问题,您可以使cmp成为static成员 function 或使用lambda或在类外部(在全局命名空间)实现cmp ,如下所示:

Method 1方法一

Make cmp a static member function.cmp成为static会员function。

class test{
    //other members 
    public:
        //make cmp static 
        static bool cmp(const pair<int, int>& p1, const pair<int, int>& p2)
        {
            return p1.second < p2.second;
        }

        void my_sort(vector <pair<int, int>> &data)
        {
//-----------------------------------------vvvvvvvvv--->cmp doesn't require an object to be called 
            sort(data.begin(), data.end(), test::cmp);
       }
};

Demo 1演示 1

Method 2方法二

Make cmp a free function.使cmp免费 function。

//free function cmp outside class test
bool cmp(const pair<int, int>& p1, const pair<int, int>& p2)
{
        return p1.second < p2.second;
}
class test 
{
   //code here as before

   void my_sort(vector <pair<int, int>> &data)
    {
//-------------------------------------vvv---->no change here
        sort(data.begin(), data.end(), cmp);
    }
};

Demo 2演示 2

Method 3方法三

You can also use lambda as shown below:您也可以使用lambda ,如下所示:

class test
{
    public:
    

    void my_sort(vector <pair<int, int>> &data)
    {
//-------------------------------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv-->use lambda
        sort(data.begin(), data.end(), [](const pair<int, int>& p1, const pair<int, int>& p2)->bool
        {
            return p1.second < p2.second;
            
        }
        );
    }
};

Demo 3演示 3

In order to use a member function with std::sort the function must be declared as static member function为了将成员 function 与 std::sort 一起使用,必须将 function 声明为 static 成员 function

To call a non-static member function you need to provide an object for which the function will be called.要调用非静态成员 function,您需要提供一个 object,function 将被调用。

Just define within the class test an operator-function and call the function std::sort within the member function my_sort the following way只需在 class test中定义一个运算符函数,然后按以下方式在成员 function my_sort中调用 function std::sort

std::sort( std::begin( data ), std::end( data ), *this );

Here is a demonstrative program.这是一个演示程序。

#include <iostream>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>

class test
{
public:
    bool operator ()( const std::pair<int, int>& p1, const std::pair<int, int>& p2 ) const 
    {
        return p1.second < p2.second;
    }

    void my_sort( std::vector<std::pair<int, int>> &data ) const
    {
        std::sort( std::begin( data ), std::end( data ), *this );
    }
};

int main()
{
    std::vector <std::pair<int, int>> data;
    for(int i=10; i>=0; i--)
    {
        data.push_back( std::pair<int, int>( 10-i, i) );
    }
    test a;
    a.my_sort(data);

    for (auto &&i : data)
    {
        std::cout << i.first << '\t' << i.second << std::endl;
    }
}

You can also call the function the following ways您也可以通过以下方式拨打function

test().my_sort( data );

or或者

std::sort( std::begin( data ), std::end( data ), test() );

Otherwise declare the member functions as static member functions.否则将成员函数声明为 static 成员函数。 For example例如

#include <iostream>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>

class test
{
public:
    static bool cmp( const std::pair<int, int>& p1, const std::pair<int, int>& p2 )
    {
        return p1.second < p2.second;
    }

    static void my_sort(std::vector <std::pair<int, int>> &data ) 
    {
        std::sort( std::begin( data ), std::end( data ), cmp );
    }
};

int main()
{
    std::vector <std::pair<int, int>> data;
    for(int i=10; i>=0; i--)
    {
        data.push_back( std::pair<int, int>( 10-i, i) );
    }

    test::my_sort(data);
//    std::sort( std::begin( data ), std::end( data ), test::cmp);

    for (auto &&i : data)
    {
        std::cout << i.first << '\t' << i.second << std::endl;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能将绑定的成员函数作为可调用函数传递? - Why can't I pass a bound member function as a callable? 为什么我不能将this指针显式传递给成员函数? - Why can't I pass the this pointer explicitly to a member function? 在VC ++调试器上计算表达式时,为什么会出现“成员函数不存在”错误? - Why do I get a “member function not present” error when evaluating expressions on the VC++ debugger? 自定义排序比较功能引发编译错误 - Custom sort compare function throws compile error 当std :: sort中的比较函数始终返回true时,为什么会出现运行时错误? - Why do I get Runtime Error when comparison function in std::sort always return true? 将函数作为参数传递给C ++中的pthread时,为什么会出现“非静态成员函数”错误? - Why do I get a “non-static member function” error when passing a function as a parameter to a pthread in C++? 如何从构造函数传递给作为成员函数的谓词 - How to pass to sort predicate that is a member function, from the constructor 为什么我不能在 C++ sort() 中使用成员函数 cmp? - Why can't I use a member function cmp in C++ sort()? 为什么在使用时不能使用成员函数来获取成员变量 <set> 迭代器? - Why can't I use the member function to get a member variable when I use <set> iterator? 为什么我得到一个<function>不是会员<class> ? 我试图传递一个字符串并从中返回 - Why am I getting a <function> is not a member of <class>? I was trying to pass a string and return from it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM