Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::sort with predicate function in Class

Tags:

c++

I want to sort vector of certain struct with certain order in certain class. I've wrote definition of struct and predicate function in a class and run std::sort in a method of the class with these struct and function. But compilation error has occurred. gcc version is 4.0.1 and OS is Mac OSX. The code is following:

class sample {
public:
  struct s {
    int x;
    int y;
  };

  bool cmp (struct s a, struct s b) {
    if (a.x == b.x)
      return a.y < b.y;
    else
      return a.x < b.x;
  }

  int func(void) {
    std::vector <struct s> vec;

    // ...

    sort(vec.begin(), vec.end(), cmp);  // compilation error

    // ...

    return 0;
  }
};

int main(void) {
  sample *smp = new sample();
  smp->func();
  return 0;
}

Error message was huge and complex. So this is first two lines of it.

sortSample.cpp: In member function 'int sample::func()':
sortSample.cpp:51: error: argument of type 'bool (sample::)(sample::s, sample::s)' does not match 'bool (sample::*)(sample::s, sample::s)'
...

Instead of above approach, the code could run correctly with following ways.

  1. Define struct s and function cmp() outside of class sample.
  2. Remove function cmp() and define operator overloading of < in struct s.

Sample code of each approach is bellow.

1)

struct s {
  int x;
  int y;
};

bool cmp (struct s a, struct s b) {
  if (a.x == b.x)
    return a.y < b.y;
  else
    return a.x < b.x;
}

class sample {
// ...

2)

struct s {
  int x;
  int y;

  bool operator<(const struct s & a) const {
    if (x == a.x)
      return y < a.y;
    else
      return x < a.x;
  }
};

Can anyone tell a mechanism of this behavior? Why does first approach invokes compilation error?

Thanks.

like image 564
kosei Avatar asked Oct 22 '09 06:10

kosei


People also ask

What is a predicate in C++?

Predicates are commonly used by very general-use functions to allow the caller of the function to specify how the function should behave by writing their own code (when used in this manner, a predicate is a specialized form of callback ). For example, consider the sort function when it has to sort a list of integers.

What is the difference between sort and sort by predicate?

Using std::sort have the exact same syntax, and the results are identical. In a written or spoken language, a predicate is a part of a sentence, which "states something" or "tells something" about a subject (or a thing). In computer science and C++, a predicate is much of the same thing.

How to sort in STL with sort () function?

// sort () in STL. How to sort in descending order? sort () takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass the “greater ()” function to sort in descending order.

How to sort in descending order using sort in C++?

sort() takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass “greater()” function to sort in descending order. This function does a comparison in a way that puts greater element before. // C++ program to demonstrate descending order sort using.


1 Answers

In the first case cmp is declared as a member function of the class sample and hence requires this pointer for calling it. Since the this pointer is not available compiler is complaining about it. You can make it work by declaring cmp as static function since static functions do not require this pointer for calling. In the second case, since cmp is declared as a stand-alone function again it will behave same as static function. In the third case (with overloaded operator), the sort algorithm will take care of calling the function for each object in the vector and hence it compiles.

like image 113
Naveen Avatar answered Oct 22 '22 11:10

Naveen