Exploration of STL function objects

Source: Internet
Author: User

Today, let's look at the functional header file in STL. The file defines some common function objects (as the name suggests, it defines the overload () operator so that a class can be used like a function ).
First, a struct is defined as the base class of other struct. In fact, this struct only provides a uniform type name for other classes to inherit.
The template of the unary struct is scheduled to be as follows:
Template <class _ A, class _ r>
Struct unary_function {
Typedef _ A argument_type;
Typedef _ r result_type;
};
The template parameter definition should be visible, one of which is the input parameter and the other is the output result. If it is binary, you don't need to mention it. It's just one more input parameter than the one dollar:
Template <class _ A1, class _ A2, class _ r>
Struct binary_function {
Typedef _ A1 first_argument_type;
Typedef _ A2 second_argument_type;
Typedef _ r result_type;
};

Next let's take a look at how other function objects are defined. Let's take greater as an example.
Template <class _ ty>
Struct greater: binary_function <_ ty, _ ty, bool> {
Bool operator () (const _ ty & _ x, const _ ty & _ y) const
{
Return (_ x> _ y );
}
};
Taking sort in STL as an example, first define an array
Int A [5] = {2, 4, 1, 3, 5 };
Sort (& A [0], & A [5]);
The output result should be 1, 2, 3, 4, 5.
However, if the function object greater is used as the third parameter of sort:
Sort (& A [0], & A [5], greater <int> ());
Then the output result is 5, 4, 3, 2, 1.
That is, the comparison method of sort adopts greater for comparison.

To fully understand function objects, a struct is defined and inherited from binary_function, Which is sorted by the absolute value.
Template <class T>
Struct absoluteless: Public binary_function <t, t, bool>
{
Bool operator () (T x, t y) const
{
Return ABS (x)> ABS (y );
}
};

Then use this function object in the bubble sort,
The Bubble Sorting is as follows:
Template <class T, class comparetype>
Void bubble_sort (T * P, int size, const comparetype & compare)
{
For (INT I = 0; I <size; ++ I)
{
For (Int J = I + 1; j <size; ++ J)
{
If (compare (P [I], p [J])
{
Const t temp = P [I];
P [I] = P [J];
P [J] = temp;
}
}
}
}

The test procedure is as follows:
Int main ()
{
Double A [7] = {-100.77,-40.2, 50.4, 23.6, 67.1,-10.3, 11.8 };
Int size = sizeof (a)/sizeof (double );
Bubble_sort (A, size, greater <double> ());
Display (A, size); // print
Bubble_sort (A, size, absoluteless <double> ());
Display (A, size );
Return 0;
}

Output result:
Bubble_sort (A, size, greater <double> ()):

67.1 50.4 23.6-11.8-10.3-40.2-100.77
Bubble_sort (A, size, absoluteless <double> ()):

-10.3 11.8-23.6 40.2 50.4-67.1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.