简体   繁体   English

在std:sort()上使用类

[英]Using class on std:sort()

An example from here: http://www.cplusplus.com/reference/algorithm/sort/ 此处的示例: http : //www.cplusplus.com/reference/algorithm/sort/

Shows that 表明

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
  int myints[] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)
}

This works fine, however I am trying to use class instead of structure. 这工作正常,但是我正在尝试使用类而不是结构。 So what I am doing is: 所以我正在做的是:

CardComparer class: CardComparer类:

bool CardComparer::operator() (Card* firstCard, Card* secondCard) {
    this->firstCard = firstCard;
    this->secondCard = secondCard;
    if (firstCard->GetRank() == secondCard->GetRank()) {
        return firstCard->GetSuit() > secondCard->GetSuit();
    }
    else {
        return firstCard->GetRank() > secondCard->GetRank();
    }
}

and this is main: 这是主要的:

CardComparer* compare;
compare = new CardComparer();
sort(cards.begin(), cards.end(), compare->operator());

I am getting this long error: 我收到这个长错误:

hand.cpp: In member function 'void Hand::AddCard(Card*)':
hand.cpp:60:54: error: no matching function for call to 'sort(std::vector<Card*>::iterator, std::vector<Card*>::iterator, <unresolved overloaded function type>)'
hand.cpp:60:54: note: candidates are:
In file included from /usr/include/c++/4.7/algorithm:63:0,
                 from hand.cpp:4:
/usr/include/c++/4.7/bits/stl_algo.h:5463:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
/usr/include/c++/4.7/bits/stl_algo.h:5463:5: note:   template argument deduction/substitution failed:
hand.cpp:60:54: note:   candidate expects 2 arguments, 3 provided
In file included from /usr/include/c++/4.7/algorithm:63:0,
                 from hand.cpp:4:
/usr/include/c++/4.7/bits/stl_algo.h:5499:5: note: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Card**, std::vector<Card*> >; _Compare = bool (CardComparer::*)(Card*, Card*)]
/usr/include/c++/4.7/bits/stl_algo.h:5499:5: note:   no known conversion for argument 3 from '<unresolved overloaded function type>' to 'bool (CardComparer::*)(Card*, Card*)'

I couldn't really find a solution because if i modify the sample and keep it as a struct it works fine, but does not work when i convert it into a class. 我真的找不到解决方案,因为如果我修改示例并将其保留为结构,它可以正常工作,但是当我将其转换为类时,它将无法正常工作。

The third argument is called a functor, and is something that can be called. 第三个参数称为函子,可以调用。 Either a pointer to a function, a C++11 lambda, or an object instance (not pointer) with an operator() member function. 指向函数的指针,C ++ 11 lambda或具有operator()成员函数的对象实例 (不是指针)。

In your case, don't dynamically allocate the functor object on the heap, it's enough to declare it as a temporary object in the std::sort call: 在您的情况下,不要在堆上动态分配functor对象,在std::sort调用中将其声明为临时对象就足够了:

std::sort(cards.begin(), cards.end(), CardComparer());

In the above std::sort call, using CardComparer() creates an object on the stack, this object is temporary and only valid while std::sort is running. 在上面的std::sort调用中,使用CardComparer()在堆栈上创建了一个对象,该对象是临时的,仅在std::sort运行时有效。 The std::sort function will call this object, which is the same as invoking the operator() function on the object. std::sort函数将调用此对象,这与在该对象上调用operator()函数相同。

Since this comparison functor is pretty simple, it doesn't need to store any data: 由于此比较函子非常简单,因此不需要存储任何数据:

struct CardComparer
{
    bool operator() (const Card* firstCard, const Card* secondCard) const { ... }
};

So no need for the member data fields. 因此,不需要成员数据字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM