#include#include #include using namespace std; int main() { vector v = {2, 3, 5, 7, 11}; int count_of_3s = std::count_if(v.begin(), v.end(), std::bind2nd(std::equal_to (), 3)); cout << "The count of 3s in the vector is: " << count_of_3s << endl; return 0; }
#includeIn this example, we are using std bind2nd to create a sorting comparator that orders the values greater than 5 before the others. We do this by binding the second argument of our custom comparison function greater_than_5 to the value 0, effectively ignoring it. Both examples use C++ Standard Library. Thus, both packages#include #include using namespace std; bool greater_than_5(int i, int j) { return i > j && i > 5; } int main() { vector v = {2, 7, 3, 9, 1, 6, 4}; std::sort(v.begin(), v.end(), std::bind2nd(std::ptr_fun(greater_than_5), 0)); for(auto i : v) cout << i << " "; return 0; }