STL function objects and lambda expressions

Source: Internet
Author: User

 

STL function objects and lambda expressions

 

1. Basic Concepts

Function object is an object that defines operator.

Functionobjecttype fo;

Fo (...); The operator () of the call function object replaces the call of the Function fo.

It is equivalent to: FO. Operator ()(...);

 

Three Benefits of function objects:

(1) function objects can have their own states, so they may be smarter. You can have two instances of the same function object, which may have different states.

(2) Each function object is a type. You can use a function object as a template parameter to specify a specific behavior.

(3) function objects are generally faster than function pointers. Because more information is defined during compilation.

 

2. Function objects have their own statuses, which are more intelligent.

For example, add a value to the execution:

(1) Add 10 to the normal function during compilation

Void add10 (Int & ELEM)

{

ELEM + = 10;
}

Void F1 ()

{

Vector <int> ivec;

...

For_each (ivec. Begin (), ivec. End (), add10 );
}

(2) If you want to add other values during compilation, You need to rewrite the common function, you can use the function template.

Template <int Val> // non-type template parameter: The compile time

Void add (Int & ELEM)

{

ELEM + = val;
}

Void F1 ()

{

Vector <int> ivec;

...

For_each (ivec. Begin (), ivec. End (),

Add <10> );
}

(3) function object can be used to increase the running value.

You can add a constant, such as 10, or a variable (the value is known at runtime ).

 

3. As a type, it can be used as a template parameter, for example, as a sorting criterion

 

4. predefined function objects

The header file # include <functional> defines some function objects: +-*/=! = <><=>=! & | ^.

Note: less <> is the default sorting criterion of the sort function associated with containers.

Performance_to <> is the default equality criterion for unordered containers.

 

5. function adapters

Function adapter is a function object that can combine function objects.

Since C ++ 11 includes:

BIND (OP, argS ...); // Binds ARGs to OP

Mem_fn (OP); // call OP () is a member function of an object or object pointer. You can also & Class: memfun

Not1 (OP );//! OP (PARAM)

Not2 (OP );//! OP (param1, param2)

The most important thing is bind (), which is of little use.

Generally, BIND () can bind an object to an object, and use STD: placeholders (GPS): _ 1, _ 2 to pass the real parameter.

 

6. Lambda expressions

Function objects are more suitable for use in libraries, and lambda is more suitable for applications and more intuitive.

 

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.