【问题标题】:How can I overload a custom std::sort comparison function?如何重载自定义 std::sort 比较函数?
【发布时间】:2013-12-01 03:19:19
【问题描述】:

使用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 可能有点模棱两可,但我假设编译器能够根据与向量u 关联的类型来解决这个问题。有什么方法可以让我指定使用哪个函数/方法来消除问题的歧义?

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

【问题讨论】:

  • 为什么比较函数的第二个参数不是 const 引用?它缺少&amp;。
  • 要手动解决过载问题,请使用static_cast。
  • 我已经编辑了我的问题。谢谢。在这种情况下如何使用“static_cast”?
  • 您必须手动将其转换为正确的函数指针类型,以帮助编译器消除调用歧义。

标签: c++ sorting vector stdvector std-pair


【解决方案1】:

您可以使用仿函数:

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

【讨论】:

  • 我从没想过你的回答,所以谢谢你。但是,我正在寻找一种方法来更改 main 中的排序行,以便我的原始类保持不变。我的问题的重点实际上是消除歧义,而不是一个完整的替代方案。
  • @slaw 正如我在编辑中指出的那样,您可以通过引用传递它,而无需更改任何其他代码。
  • 通过引用传递它没有编译,也没有必要。相反,只需注释掉第一个 sortPair 函数编译并相应地对向量进行排序(即它消除了歧义)。虽然,我仍然不清楚如何在不注释第一个 sortPair 的情况下让代码识别要使用的函数。
【解决方案2】:

由于u 是std::pair&lt;double, std::string&gt; 的向量,您需要调用相应的比较函数。由于仅名称是不够的,您必须通过将其转换为具有正确函数指针类型的指针来为编译器消除歧义。在您的情况下,它是一个函数,它需要对 pair 类型的两个 const 引用并返回一个 bool - 所以您必须转换为的函数指针类型正是:

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

合起来就是一个非常丑陋的演员阵容:

std::sort(u.begin(), u.end(), static_cast<bool (*)(const std::pair<int, int> &, const std::pair<int, int> &)>(&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;
}

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

【讨论】:

  • 在 (&Misc::sortPair) 中是否需要 & 符号?我通常会忽略它,这是我第一次看到它在排序比较函数中使用。
  • @slaw 不,没有必要,函数引用将隐式转换为函数指针。我只是想明确指出使用了函数指针。
【解决方案3】:

由于C++11,你也可以使用lambda expressions来代替定义比较函数。这样,您既不需要 Misc 类,也不需要担心函数消歧:

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

Code on Ideone

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多