Chapter 7 heavy-duty operators and conversions (7)

Source: Internet
Author: User

14.8 call operators and function objects
Struct absInt {
Int operator () (int val ){
Return val <0? -Val: val;
}
};
Int I = 100;
AbsInt absObj;
Cout <absObj (I) <endl;
Function call operators must be declared as member functions. A class can be defined as multiple versions of the function call operator, which are differentiated by the number or type of parameters.
A class that defines call operators. Its objects are often called function objects, which act like functions.
14.8.1 use function objects in standard library Algorithms
Function objects are often used as arguments for common algorithms.
1. Function objects can be more flexible than Functions
Class GT_cls
{
Public:
GT_cls (size_t val = 0): bound (val ){}
Bool operator () (const string & s)
{
Return s. size ()> = bound;
}
Private:
String: size_type bound;
};
2. Use the GT_cls function object
GT_cls gt (6 );
Cout <gt (string ("1231244") <endl;
14.8.2 function objects defined in the standard library
The standard library also defines a set of function adapters so that we can customize or extend the function object classes defined in the standard library. These standard library function object types are defined in the functional header file.
1. Each class represents a given Operator
There are two unary function objects: unary function objects (negate) and non-logical (logical not <type> ). Other standard library function objects are binary function objects that represent binary operators.
2. indicates the template type of the operand type.
Plus <int> intadd;
Cout <intadd (1, 2) <endl;
3. Use standard library function objects in algorithms
Vector <int> vec;
Sort (vec. begin (), vec. end (), greater <int> ());
The third real parameter is used to pass the predicate function of the comparison element.
14.8.3 function adapter of the function object
The standard library provides a set of function adapters (ingke) for the special and extended objects of mona1 and binary functions. Function adapters are divided into the following two types:
(1) binder is a function adapter that binds an operand to a given value and converts a binary function object to a one-dimensional function object.
(2) negator is a function adapter that returns the true value of a predicate function object.
The standard library defines two biner adapters: bind1st and bind2nd. Bind1st binds a value to the first real parameter of the binary function object, and bind2nd binds the value to the second real parameter of the binary function object.
Greater <int> gre;
Cout <gre (10, 0) <endl; // 1
Cout <bind1st (greater <int> (), 10) (0) <endl; // 1 www.2cto.com
Cout <gre (10, 20) <endl; // 0
Cout <bind1st (greater <int> (), 10) (20) <endl; // 0
The standard library also defines two backend servers: not1 and not2. Not1 returns the true value of the mona1 function object, while not2 returns the true value of the binary function object.
Cout <not2 (less <int> () (10, 20) <endl; // 0

From xufei96's column

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.