Why does the code not work if instead of 3 you insert a number less than 6. And how can this be corrected? The code should sort the vector using the std :: greater functor, which should use 1 parameter. You need to do this with std :: bind.

#include <set> #include <algorithm> #include <iostream> #include <vector> #include <list> #include <map> #include <iterator> #include <string> #include <functional> using namespace std; template<typename T> static void PrintVector(const std::vector<T> &v) { for (auto iterator = v.begin(); iterator != v.end(); ++iterator) { std::cout << *iterator << " "; } std::cout << std::endl; } int main() { vector<int> v = { 1, 8, 7, 4, 3, 6, 2, 5 }; PrintVector(v); auto greater_binded = bind(greater<int>(), placeholders::_1, 3); sort(v.begin(), v.end(), greater_binded); PrintVector(v); return 0; } 
  • 2
    "Why is the code not working ...?" And where in your question is a meaningful description of what this code should do at all? No sane explanation can be extracted from the code itself - it is meaningless. - AnT

2 answers 2

The std::sort algorithm assumes strict weak ordering for comparison functions (25.4 Sorting and related operations)

3 For all algorithms that you use. That is, comp (* i, * j)! = False defaults to * i <* j! = False. For compat- ies, it is possible to work correctly.

This means that it is not allowed that if comp( a, b ) is true, then comp( b, a ) also true at the same time.

If you want to partition the array into partitions, use the standard std::partition algorithm. For example,

 std::partition( v.begin(), v.end(), greater_binded ); 

If necessary, then each partition can be sorted separately.

  • It seems to read a couple of days ago about this on google groups. And it seems it was you :) - isnullxbh
  • @isnullxbh On google, I mainly participate in the isocpp.org forums to discuss the C ++ standard. - Vlad from Moscow

And what should this code do? std::sort accepts a binary predicate. Something with which you can do this:

 if(pred(x, y)){ //... } 

You took the binary predicate greater and zabindili one argument. In theory, it now takes only one argument. Although it is not actually specified that it returns std::bind ( example )

In short, although the code is compiled, it will not work correctly in this form. Because the predicate greater_binded works with only one argument, and for sorting it is necessary to compare two elements.

You can easily see this if you replace std::bind with the good old std::bind2nd . A runtime error will immediately turn into a compilation error:

 #include <algorithm> #include <iostream> #include <vector> #include <functional> using namespace std; template<typename T> static void PrintVector(const std::vector<T> &v) { for (auto iterator = v.begin(); iterator != v.end(); ++iterator) { std::cout << *iterator << " "; } std::cout << std::endl; } int main() { vector<int> v = { 1, 8, 7, 4, 3, 6, 2, 5 }; PrintVector(v); auto greater_binded = bind2nd(greater<int>(), 3); sort(v.begin(), v.end(), greater_binded); PrintVector(v); } 

h: 125: 23: error: no return stol :: binder2nd> return return (_M_comp (* __ it1, * __ it2)); }