template <class T> // A template class taking any type T
// This class inherit from std::binary_function
struct greater : binary_function <T, T, bool>
{
// This is a struct (not a class).
// It means members and inheritens is public by default
// This method defines operator() for this class
// you can do: greater<int> op; op(x,y);
bool operator() (const T& x, const T& y) const {
// method is const, this means you can use it
// with a const greater<T> object
return x > y; // use T::operator> const
// if it does not exist, produces a compilation error
}
};
यहाँ std::binary_function
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
की परिभाषा यह आप binary_function
greater<int> op;
greater<int>::result_type res = op(1,2);
जो
std::result_of<greater<int>>::type res = op(1,2);
के बराबर है परिभाषित करने प्रकार तक पहुंचने देता है यह क्या के बारे में ? एक उपयोग 'std :: sort (start (arr), end (arr), std :: अधिक()) हो सकता है; 'उच्चतम से निम्नतम तक पूर्णांक के कंटेनर को सॉर्ट करने के लिए। –
chris