C ++-like functions

Source: Internet
Author: User

 

Function imitation (functor, function object)

The function parameter passed to the STL algorithm (functional arguement) is not necessarily a function. It can act like a function object, that is, a function object or functor.

STL uses a lot of function objects and provides many pre-defined function objects.

If you define an object and its behavior is like a function, you can use it as a function.
So what is functional behavior?
Function behavior: You can use parentheses to pass Parameters and call something.
For example, function (arg1, arg2 );

To implement this in C ++, you only need to define operator () and give the appropriate parameter type
Similar to the following definition:
Class functor
{
Public:
Return-value operator (arguments) const;
};

Now we can call this type of object as a function:
Functor F;
F (arg1, arg2); // equivalent to F. Operator (arg1, arg2 );

 

Example of the most basic function simulation:

/**

Function object

*/

Class printint
{
Public:
Void operator () (int elem) const
{
STD: cout <ELEM <'';
}
};

Void testprintint ()
{
STD: vector <int> intvec;
For (INT I = 1; I <10; ++ I)
Intvec. push_back (I );
For_each (intvec. Begin (), intvec. End (),
Printint (); // printint () generates a temporary object of this type, when a parameter of the for_each Algorithm
STD: cout <STD: Endl;
}

The for_each () algorithm in STL is roughly implemented as follows:
Template <typename iterator, typename operation>
Operation for_each (iterator B, iterator E, Operation OP)
{
While (B ++! = E)
{
OP (* B );
}
Return op;
}

In this example, for_each () calls printint: Operator (* B );

Advantages of function imitation:
1. the imitation function is an object that can have member functions and member variables, that is, the imitation function has state (states)
2. Each function has its own type.
3. the imitation function is usually faster than the general function (a lot of information is determined during compilation)

For example, to add a fixed value to each element in the cluster

/**
General functions
The user expects to know this number during compilation.
*/
Void add10 (Int & value)
{
Value + = 10;
}

/**
Function Template
Different fixed values are required, and they are determined during the compilation period.
*/
Template <int thevalue>
Void addvalue (Int & value)
{
Value + = thevalue;
}

/**
Imitation Functions
*/
Class addvalue
{
PRIVATE:
Int value;
Public:
Addvalue (int v): Value (v ){}
Void operator () (Int & ELEM) const
{
ELEM + = value;
}
};

Void testaddvalue ()
{
STD: List <int> intlist;
For (INT I = 1; I <10; ++ I)
Intlist. push_back (I );
STD: cout <"initialized: \ n ";
Copy (intlist. Begin (), intlist. End (),
STD: ostream_iterator <int> (STD: cout ,""));

For_each (intlist. Begin (), intlist. End (),
Add10); // call the function. The compilation period is 10.
STD: cout <"\ nafter add10: \ n ";
Copy (intlist. Begin (), intlist. End (),
STD: ostream_iterator <int> (STD: cout ,""));

For_each (intlist. Begin (), intlist. End (),
Addvalue <11>); // call the function template. The compilation period is 11.

STD: cout <"\ nafter addvalue: \ n ";
Copy (intlist. Begin (), intlist. End (),
STD: ostream_iterator <int> (STD: cout ,""));

// For_each (intlist. Begin (), intlist. End (),
// Addvalue <* (intlist. Begin ()>); // error, because the template must know the template parameters during compilation, which is only known at runtime.
// STD: cout <"\ nafter addvalue: \ n ";
// Copy (intlist. Begin (), intlist. End (),
// STD: ostream_iterator <int> (STD: cout ,""));

For_each (intlist. Begin (), intlist. End (),
Addvalue (15 ));
STD: cout <"\ nafter addvalue 15: \ n ";
Copy (intlist. Begin (), intlist. End (),
STD: ostream_iterator <int> (STD: cout ,""));

For_each (intlist. Begin (), intlist. End (),
Addvalue (* intlist. Begin (); // advantages of function templates during execution !!
STD: cout <"\ nafter addvalue dynamically: \ n ";
Copy (intlist. Begin (), intlist. End (),
STD: ostream_iterator <int> (STD: cout ,""));
}

STL provides many predefined imitation functions, such:

Performance_to, not_equal_to, less, greater, less_equal, greater_equal, nagate, multiplies, etc.

Below are several examples of running STL pre-defined functions:

Void testpredeffunctor ()
{
STD: List <int> intlist;
STD: Set <int, STD: greater <int> intset;
For (INT I = 1; I <10; ++ I)
{
Intlist. push_back (I );
Intset. insert (I );
}
Intlist. Sort (STD: greater <int> (); // sort data in ascending order.
Transform (intlist. Begin (), intlist. End (), // source1
Intlist. Begin (), // source2
Intlist. Begin (), // dest
STD: multiplies <int> (); // operation
}

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.