C++ 如何重载自定义std::sort比较函数?
使用C++ 如何重载自定义std::sort比较函数?,c++,sorting,vector,stdvector,std-pair,C++,Sorting,Vector,Stdvector,Std Pair,使用std::sort时,如何重载正在使用的自定义比较函数 #include <string> #include <vector> #include <iostream> #include <algorithm> class Misc { public: // Comment out the next three lines to compile without problems. static bool sortPair(const s
std::sort
时,如何重载正在使用的自定义比较函数
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
class Misc {
public:
// Comment out the next three lines to compile without problems.
static bool sortPair(const std::pair<int, int> &a, const std::pair<int, int> &b){
return a.first < b.first;
}
static bool sortPair(const std::pair<double, std::string> &a, const std::pair<double, std::string> &b){
return a.first < b.first;
}
};
int main () {
std::vector<std::pair<double, std::string> > u;
u.push_back(std::make_pair(10.0, "ten"));
u.push_back(std::make_pair(5.0, "five"));
u.push_back(std::make_pair(1.0, "one"));
std::sort(u.begin(), u.end(), Misc::sortPair);
for (unsigned int i=0; i< u.size(); i++){
std::cout << u.at(i).first << std::endl;
}
return 0;
}
#包括
#包括
#包括
#包括
类杂项{
公众:
//注释掉接下来要编译的三行,没有问题。
静态布尔sortPair(常数std::pair&a,常数std::pair&b){
返回a.first std::cout您可以改用函子:
class Misc {
public:
// static bool sortPair(const std::pair<int, int> &a, const std::pair<int, int> &b);
// static bool sortPair(const std::pair<double, std::string> &a, const std::pair<double, std::string> &b);
bool operator() (const std::pair<int, int> &a, const std::pair<int, int> &b) { // something
return true;
}
bool operator() (const std::pair<double, std::string> &a, const std::pair<double, std::string> &b) { // something
return true;
}
} misc;
int main()
{
std::vector<std::pair<double, std::string> > u;
std::vector<std::pair<int, int> > u2;
//Fill vector u with data
std::sort(u.begin(), u.end(), misc);
std::sort(u2.begin(), u2.end(), misc);
return 0;
}
class杂项{
公众:
//静态布尔sortPair(const std::pair&a,const std::pair&b);
//静态布尔sortPair(const std::pair&a,const std::pair&b);
bool操作符()(const std::pair&a,const std::pair&b){//something
返回true;
}
bool操作符()(const std::pair&a,const std::pair&b){//something
返回true;
}
}杂项;
int main()
{
std::向量u;
std::向量u2;
//用数据填充向量u
std::sort(u.begin()、u.end()、misc);
std::sort(u2.begin(),u2.end(),misc);
返回0;
}
由于u
是std::pair
的向量,您需要调用相应的比较函数。由于名称本身是不够的,您必须通过将它转换为具有正确函数指针类型的指针来为编译器消除歧义。在您的情况下,它是一个需要两个常量引用的函数返回pair类型并返回bool-因此您必须强制转换到的函数指针类型正好是:
bool (*)(const std::pair<int, int> &, const std::pair<int, int> &)
bool(*)(常数std::pair&,常数std::pair&)
一起组成了一个非常丑陋的演员阵容:
std::sort(u.begin(), u.end(), static_cast<bool (*)(const std::pair<int, int> &, const std::pair<int, int> &)>(&Misc::sortPair));
std::sort(u.begin()、u.end()、static_cast(&Misc::sortPair));
哇
最好使用一些typedef来说明您正在做什么:
//includes as they were...
typedef std::pair<double, std::string> dsPair; //or something more meaningful
class Misc {
public:
//Comment out the next three lines to compile without problems
static bool sortPair(const std::pair<int, int> &a, const std::pair<int, int> &b){
return a.first < b.first;
}
static bool sortPair(dsPair const& a, dsPair const& b){
return a.first < b.first;
}
};
int main () {
std::vector<dsPair> u{
{10.0, "ten"},
{5.0, "five"},
{1.0, "one"}
};
/** the function pointer typedef
* It takes a bit getting used to, but no worries,
* you won't have to do it THAT often:
**/
typedef bool(*dsPairCompFunc)(dsPair const&, dsPair const&);
//and now the cast is much clearer:
std::sort(begin(u), end(u), static_cast<dsPairCompFunc>(&Misc::sortPair));
for (auto& e : u){
std::cout << e.first << "\n";
}
return 0;
}
//按原样包含。。。
typedef std::pair dsPair;//或更有意义的内容
类杂项{
公众:
//注释掉接下来要编译的三行,没有问题
静态布尔sortPair(常数std::pair&a,常数std::pair&b){
返回a.firstMisc
类,也不用担心函数消歧:
int main() {
using dbl_str_pair_t = std::pair<double, std::string>;
std::vector<dbl_str_pair_t> u {{10.0, "ten"}, {5.0, "five"}, {1.0, "one"}};
using int_int_pair_t = std::pair<int, int>;
std::vector<int_int_pair_t> v {{3, 4}, {1, 5}, {2, 6}};
std::sort(std::begin(u), std::end(u), [](const dbl_str_pair_t& a, const dbl_str_pair_t& b) {
return a.first < b.first;
});
std::sort(std::begin(v), std::end(v), [](const int_int_pair_t& a, const int_int_pair_t& b) {
return a.first < b.first;
});
for (auto const &p : u)
std::cout << p.first << std::endl;
for (auto const &p : v)
std::cout << p.first << std::endl;
return 0;
}
intmain(){
使用dbl\u str\u pair\u t=std::pair;
向量u{{10.0,“十”},{5.0,“五”},{1.0,“一”};
使用int_int_pair_t=std::pair;
向量v{{3,4},{1,5},{2,6};
标准::排序(标准::开始(u),标准::结束(u),[](常数dbl\u str\u pair\u t&a,常数dbl\u str\u pair\u t&b){
返回a.first std::cout为什么比较函数的第二个参数不是常量引用?它缺少&
。若要手动解决重载问题,请使用static\u cast
。我已编辑了我的问题。谢谢。如何使用“static\u cast”在这个例子中?可能的重复必须手动将其转换为正确的函数指针类型,以帮助编译器消除调用的歧义。我从来没有想过你的答案,所以谢谢你。但是,我正在寻找一种方法来更改main中的排序行,以便我的原始类保持不变。我的问题的重点是e消歧,而不是一个完整的替代方法。@slaw正如我在编辑中指出的,您可以通过引用传递它,而无需更改任何其他代码。通过引用传递它没有编译,也没有必要。相反,只需注释编译的第一个sortPair函数并相应地对向量排序(即,它消除了歧义).尽管如此,我仍然不清楚如何让代码在不注释第一个sortPair的情况下识别要使用的函数。(&Misc::sortPair)中是否需要使用符号AND?我通常会忽略此项,这是我第一次在排序比较函数中看到它的使用。@slaw no这不是必需的,函数引用将隐式转换为函数指针。我只想显式地使用函数指针。