#include <algorithm>#include <iostream>#include <vector>#include <functional>struct Comp : public std::binary_function<int, int, bool>{ Comp() {} Comp(const Comp& old) { std::cout << "copy constructor" << std::endl; } bool operator () (int lhs, int rhs) const { return lhs < rhs; }};template <typename Fn2>class binary_function_ref : public std::binary_function<typename Fn2::first_argument_type, typename Fn2::second_argument_type, typename Fn2::result_type> {public: explicit binary_function_ref(const Fn2& fn2) : fn2(fn2) { } typename Fn2::result_type operator()(const typename Fn2::first_argument_type& arg1, typename Fn2::second_argument_type& arg2) { return fn2(arg1, arg2); }private: const Fn2& fn2;};template <typename Fn2>binary_function_ref<Fn2> make_ref(const Fn2& fn2){ return binary_function_ref<Fn2>(fn2);}int main(int argc, char** argv){ std::vector<int> big; for (int i = 1000; i; --i) big.push_back(i); std::sort(big.begin(), big.end(), make_ref(Comp())); return 1;}
[Edited by - rozz666 on June 26, 2008 9:29:49 AM]