0

How to sort vector<int> v{3,2,7,6,5,8,1,2,3,4,5}; in decreasing order using function object.
I did using lambda expression
sort(v.begin(),v.end(),[](int a , int b){return a > b;});

But How we do using fn object

2
Contributors
1
Reply
13
Views
4 Years
Discussion Span
Last Post by mike_2000_17
0

Here is a basic function object:

struct greater_than {
  bool operator()(int a, int b) const {
    return a > b;
  };
};

and the sort call just becomes:

sort(v.begin(), v.end(), greater_than());

where greater_than() just creates the function object that is passed to the sort function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.