-1

I have a custom compare function:

bool compare(int a, int b){
    if(a>2*b) return true;
    return false;
}

now I want to use this function like this:

vector<int> numbers;//say it contains random numbers
sort(numbers.begin(), numbers.end(), compare(a, b));//a and b are the numbers that sort function currently compares to each other

Obviously, this code won't work, because the program doesn't know what a and b are. My question is, how do I pass the required numbers to compare function?

1 Answers1

0

There is a good answer here on how to use std::sort

https://stackoverflow.com/a/1380496/6115571

On another note, you don't really need two return statements in your function there

bool compare(int a, int b) {
  return (a>2*b);
}
MattyJacques
  • 33
  • 1
  • 5
  • You also don't need the parentheses around the expression that's being returned. `return a>2*b;` does exactly the same thing. Some people like the parentheses, but that's strictly a matter of style. – Pete Becker Feb 16 '18 at 12:52