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; }