如何重载自定义 std::sort 比较函数? [英] How can I overload a custom std::sort comparison function?

查看:44
本文介绍了如何重载自定义 std::sort 比较函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 std::sort 时,如何重载我正在使用的自定义比较函数?

When using std::sort, how can I overload the custom comparison function that I am using?

#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;
}

我无法编译它,因为它抱怨:

I can't get this to compile as it complains about:

未解析的重载函数类型

我可以看到使用 sortPair 可能有点含糊不清,但我认为编译器能够根据与向量 u 关联的类型来解决这个问题.有什么方法可以指定使用哪个函数/方法来消除问题的歧义?

I can see that using sortPair could be somewhat ambiguous, but I assumed that the compiler would be able to resolve this based on the types associated with the vector u. Is there some way that I could specify which function/method to use in order to disambiguate the problem?

目前,注释掉第一个 sortPair 函数允许编译代码并生成正确的排序输出.当然,这是因为不再模棱两可了.

Currently, commenting out the first sortPair function allows the code to be compiled and produces the correct sorted output. Of course, this is because it is not longer ambiguous.

推荐答案

由于 ustd::pair 的向量,您将需要调用相应的比较函数.由于仅名称是不够的,您必须通过将其转换为具有正确函数指针类型的指针来为编译器消除歧义.在您的情况下,它是一个函数,它接受对对类型的两个 const 引用并返回一个 bool - 因此您必须强制转换为的函数指针类型正是:

Since u is a vector of std::pair<double, std::string>, you will want to have the corresponding comparison function called. Since the name alone is not sufficient, you will have to disambiguate it for the compiler by casting it to a pointer with the right function pointer type. In your case it is a function that takes two const references to the pair type and returns a bool - so the function pointer type you have to cast to is exactly that:

bool (*)(const std::pair<int, int> &, const std::pair<int, int> &)

加在一起,演员阵容非常丑陋:

Together that makes a pretty ugly cast:

std::sort(u.begin(), u.end(), static_cast<bool (*)(const std::pair<int, int> &, const std::pair<int, int> &)>(&Misc::sortPair));

哇.

最好使用一些 typedef 来阐明您在做什么:

Better use some typedefs to clarify what you are doing:

//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;
}

我将一些旧的 C++03 内容更改为 C++11,以防您的编译器支持它.

I changed some of the old C++03 stuff to C++11 in case your compiler supports it.

这篇关于如何重载自定义 std::sort 比较函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆