[英]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
,如下所示:
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);
}
};
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);
}
};
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;
}
);
}
};
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.