C ++ std列表排序,具有取决于对象实例的成员变量的自定义比较器 [英] C++ std list sort with custom comparator that depends on an member variable for the object instance

查看:262
本文介绍了C ++ std列表排序,具有取决于对象实例的成员变量的自定义比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类:

Class:
  private:
    ...
    vector<string> words; 
    vector< list<int> > vints;
  public:
    myFunction(...)

在非空列表中的另一个成员函数:

I am calling a sort on non-empty list in another member function:

void myClass::myFunction (...) {
    ...
    if (!vints[i].empty()) vints[i].sort(sortFunc);
    ...
}

我的排序功能:

bool myClass::sortFunc(const int& i, const int& j) { return (words[i] < words[j]); }

错误:

error: no matching function for call to ‘std::list<int, std::allocator<int>      >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.4/bits/list.tcc:301: note: candidates are: void std::list<_Tp,     _Alloc>::sort() [with _Tp = int, _Alloc = std::allocator<int>]
/usr/include/c++/4.4/bits/list.tcc:378: note:                 void std::list<_Tp, _    Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (SuperWordSearch::*)    (const int&, const int&), _Tp = int, _Alloc = std::allocator<int>]

我已经研究过并遇到以下问题:

I have researched and come across the following questions:

list + sort的C ++自定义比较函数

对指针列表进行排序问题

std :: list :: sort中的错误与自定义比较器(期望的主表达式'之前')令牌)

,它们已经足够如果没有这个事实,在这个类中,sortFunc取决于成员变量WORDS对象的实例。所以我不能使比较器函数(sortFunc)静态或全局

and they would have been sufficient had it not been for the fact that in this class, the sortFunc depends on the member variable WORDS for that instance of the object. So I cannot make the comparator function (sortFunc) static or global

编辑:刚刚遇到这个如何在需要成员数据时对std:list进行排序?,它通过建立一个friend类提供一个解决方案,但是是否可以在用户定义的类本身内部完成此操作?

Just came across this How to sort a std:list when you need member data? and it provides a solution by making a friend class, but is it possible to accomplish this inside the user-defined class itself?

推荐答案

@ Kerrek的答案涉及lambdas更好。但是,如果你必须避免C ++ 11的功能,然后用函子替换你的排序函数。允许函数存储对任何所需数据的引用,如下所示:

@Kerrek's answer involving lambdas is better. But, if you must avoid C++11 features, then replace your sort function with a functor. Allow that functor to store a reference to whatever data is required, as so:

#include <vector>
#include <list>
#include <string>

class myClass {
private:
  std::vector<std::string> words;
  std::vector<std::list<int> > vints;

  // Instead of sortFunc, use sortFunctor. A functor can be used in place 
  // of a function in many places, and it can carry state (like a reference
  // to the data it needs).
  struct sortFunctor {
    const std::vector<std::string>& words;
    sortFunctor(const std::vector<std::string>& words) : words(words) { }
    bool operator()(int i, int j) { return words[i] < words[j]; }
  };

public:
  void myFunction() {
    vints[0].sort(sortFunctor(words));
  }
  myClass() {
    words.push_back("apple");
    words.push_back("berry");
    std::list<int> l;
    l.push_back(0);
    l.push_back(1);
    vints.push_back(l);
  }
};

int main () {
  myClass object;
  object.myFunction();
}

这篇关于C ++ std列表排序,具有取决于对象实例的成员变量的自定义比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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