predefined function objects and function adapters

Source: Internet
Author: User
Tags modulus

1 Basic concepts of predefined function objects: Standard Template Library STL Many predefined function objects are defined in advance,#include <functional> must be included.

//1 Using predefined function objects://the implementation of class template plus<>: Different types of data for addition operationsvoidMain () {plus<int>Intadd; intx =Ten; inty = -; intz = Intadd (x, y);//equivalent to x + ycout << z <<Endl; Plus<string>Stringadd; stringMyc = Stringadd ("AAA","BBB"); cout<< Myc <<Endl; Vector<string>v1; V1.push_back ("BBB"); V1.push_back ("AAA"); V1.push_back ("CCC"); V1.push_back ("zzzz"); //By Default, sort () arranges the elements of the container with less than the operation ascending of the underlying element type. //in order to be descending, you can pass a predefined class template greater, which invokes the greater-than operator of the underlying element type:cout <<"sort () function sorting"<<Endl;; Sort (V1.begin (), v1.end (), Greater<string> ());//from big to small     for(vector<string>::iterator It=v1.begin (); It!=v1.end (); it++) {cout<< *it <<Endl; }}

2 The predefined function objects of an arithmetic function object Support addition, subtraction, multiplication, addition, redundancy, and negation. The invoked operator is an instance associated with type
Addition:plus<types>
plus<string>= Stringadd (SVA1,SVA2);
Subtraction:minus<types> multiplication:multiplies<types> Division divides<tpye> finding remainder:modulus<tpye>
Take anti-:negate<type>
negate<int>= intnegate (ires), ires= Unaryfunc (negate<int> (), IVAL1);

3 ) Relationship Function Object

equals equal_to<tpye>

Equal_to<string> stringequal;

Sres = Stringequal (SVAL1,SVAL2);

Not equal to not_equal_to<type>

Greater than greater<type>

Greater than or equal to greater_equal<type>

Less than less<type>

Less than or equal to less_equal<type>

voidmain () {vector<string>v1; V1.push_back ("BBB"); V1.push_back ("AAA"); V1.push_back ("CCC"); V1.push_back ("zzzz"); V1.push_back ("CCC"); stringS1 ="CCC"; //int num = count_if (V1.begin (), V1.end (), equal_to<string> (), S1);    intnum = count_if (V1.begin (), V1.end (), bind2nd (equal_to<string>(), S1)); cout<< Num <<Endl;} 

4 ) Logical Function Object

Logic and Logical_and<type>

logical_and<int>= Intand (ival1,ival2);d res=binaryfunc (logical_and<  Double> (), Dval1,dval2);

Logical OR Logical_or<type>

Logical non-logical_not<type>

logical_not<int>= intnot (ival1);D res=unaryfunc (logical_not<double >,DVAL1);

function Adapter

1 ) Theoretical knowledge of function adapters

2 ) Common function function Adapter

The standard library provides a set of function adapters that specialize or extend a unary and two-tuple function object. Common adapters are:

1 Binder (Binder): Binders Convert a $ two function object to a unary function object by binding an argument to a special value. The C + + standard library provides two predefined binder adapters: bind1st and bind2nd, which bind values to the first argument of a two-tuple function object, which is bound on the second argument.

2 Counter (negator): Negator is a function adapter that flips the value of a function object. The standard library provides two predefined ngeator adapters: Not1 flips The true value of a unary predefined function object, and Not2 flips the true value of a two-tuple predicate function.

A list of commonly used function adapters is as follows:

bind1st (OP, value)

bind2nd (OP, value)

Not1 (OP)

Not2 (OP)

Mem_fun_ref (OP)

Mem_fun (OP)

Ptr_fun (OP)

Example:classisgreat{ Public: Isgreat (inti) {m_num=i; }    BOOL operator()(int&num) {        if(Num >m_num) {            return true; }        return false; }protected:Private:    intM_num;};voidmain () {vector<int>v1;  for(intI=0; i<5; i++) {V1.push_back (i+1); }     for(vector<int>::iterator it = V1.begin (); It!=v1.end (); It + +) {cout<< *it <<" " ; }    intNUM1 = count (V1.begin (), V1.end (),3); cout<<"NUM1:"<< NUM1 <<Endl; //the number of more than 2 is evaluated by the predicate    intnum2 = count_if (V1.begin (), V1.end (), Isgreat (2)); cout<<"num2:"<< num2 <<Endl; //the number of greater<int> () that is greater than 2 is calculated by using a predefined function object with 2 parameters//param > 2    intnum3 = count_if (V1.begin (), V1.end (), bind2nd (greater<int> (),2 ) ); cout<<"num3:"<< num3 <<Endl; //the modulus can be divisible by 2 for odd numbers.    intNUM4 = count_if (V1.begin (), V1.end (), bind2nd (Modulus <int> (),2 ) ); cout<<"Odd num4:"<< num4 <<Endl; intNUM5 = count_if (V1.begin (), V1.end (), Not1 (bind2nd (Modulus <int> (),2 ) ) ); cout<<"even NUM5:"<< NUM5 <<Endl; return ;}


Design concept of STL's container algorithm iterator

1) STL container through class template technology, to achieve the separation of data type and container model

2) STL's iterator technology realizes the unified method of traversing container, and provides the basis for the unification of STL algorithm.

3) STL algorithm, through the function object realizes the custom data type arithmetic operation, therefore: The STL algorithm also provides the unification.

Core idea: Actually the function object is the callback function, the idea of the callback function: It is the writer of the task and the caller of the task to effectively decouple. function pointers do function arguments.

4) Specific example: the input of the transform algorithm, through the iterator first and last point to the meta-calculation as input, through result as output, through the function object to do the operation of the custom data type. 、




predefined function objects and function adapters

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.