8. Generic program design and C + + Standard Template Library 5. Function object

Source: Internet
Author: User

1. Function object

The function object is the fourth type of main component provided by STL, which makes the application of STL more flexible and convenient, which enhances the universality of the algorithm. Most STL algorithms can use a function object as a parameter. The so-called "function object" is actually a function-like object, it can not need parameters, can also have a number of parameters, its function is to get a value, or change the state of the Operation .

In C + + programming, any ordinary function and any class that overloads the call operator operator () satisfies the characteristics of the function object, so it can be passed as a function object to the algorithm as an argument.

Taking the numerical algorithm accumulate () as an example, this paper introduces the design and application process of the function object.

The invocation format of the accumulate () algorithm consists of two types: the first invocation format is the "+" operator as the operation rule, and the second algorithm allows the user to pass the drum to the corresponding function object of the algorithm to specify the calculation rule.

In general, a user-designed normal function is one of the simplest function objects,

Example 1: Defining a Function object with a normal function

#include <iostream>

#include <numeric>//Include numeric algorithm header file

using namespace Std;

int mutl (int x, int y)

{return x + y;} Define a common function

int main () {

int a[] = {1,2,3,4,5};

const int N = sizeof (A)/sizeof (int);

cout << "The result by multipling all elements in a" << accumulate (A, A + N, 1, mutl);

GetChar ();

cout<<endl;//the common function mutl to the general algorithm

}

In addition to normal functions, another type of function object can be an object of a class and overload the function call operator in the definition.

Example 2: Rewrite the program above

#include <iostream>

#include <numeric>//Include numeric algorithm header file

using namespace Std;

Class Multclass//define MULTCLASS classes

{public:

int operator () (int x, int y) Const{return x*y;} Overloaded operator operator ()

};

int main ()

{

int a[] = {1, 2, 3, 4, 5};

const int N = sizeof (A)/sizeof (int);

cout << "The result by multipling all elements in a" << accumulate (A, A + N, 1, Multclass ());//Class Multclas S passed to the general algorithm

GetChar ();

cout << endl;//Pass the common function mutl to the general algorithm

}

Analysis: By overloading the operator operator () in class Multclass, you define an object that can be used as a function parameter, and you can also use the object as if it were a normal function mutl. But the object passed to the algorithm accumulate is obtained by the Multclass class's default constructor Multclass (), which can be supplied automatically by the compiler. function objects defined using the form of a class can carry more additional information than normal functions.

In addition to the above, the STL also defines a number of standard function objects, if the function division, can be divided into arithmetic operations, relational operations, logic operations three categories. In order to invoke these standard function objects, you need to include the header file <functional>. The standard function object is an inline function.

2. Function adapter

Similar to the Delle adapters described earlier, function adapters can also help create more kinds of function objects. The use of a function adapter is a much easier way to create a new function object than a direct bronze drum, a structure or class definition.

8. Generic program design and C + + Standard Template Library 5. Function object

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.