C ++ lambda expressions, functions, and bind usage

Source: Internet
Author: User

In the project, if you want to add a "behavior" in the class, it may take many steps to code.

For example, for A certain type of A, the update function needs to call A bunch of similar functions, and these functions need initialization and other operations. A lot of code is required, which is really annoying for me to wait for A lazy person. For example, Class

A.h


1 class
2 {
3 public:
4 ();
5 ~ A ();
6 void update ();
7 private:
8 bool fun1 ();
9 bool fun2 ();
10 bool fun3 ();
11
12 void run (const string & s );
13}
For example, you want to call fun1 () ~ in the update function of ()~ FunN (). If true is returned, run:

? If (fun1 () {run ("fun1") ;}if (fun2 () {run ("fun2") ;}if (fun3 ()) {run ("fun3 ");}

Of course, you can write N classes without any effort. Each class has fun and run and is implemented using the base class + polymorphism. This document only describes the lazy usage. I add A vector in A and traverse all functions:

 


Class
{
Public:
Struct CALL {
Function <bool (void)> func;
String name;
};

A ();
~ A ();
Void update ();
Private:

Vector <CALL> cache;

Bool fun1 ();
Bool fun2 ();
Bool fun3 ();

Void run (const string & s );
}
 


1 A: ()
2 {
3 CALL f1, f2, f3;
4 f1.name = "fun1 ";
5 f2.name = "fun2 ";
6 f3.name = "fun3 ";
7
8 f1.func = bind (& A: fun1, this );
9 f2.func = bind (& A: fun2, this );
10 f3.func = bind (& A: fun3, this );
11}
12
13 void A: update ()
14 {
15 for (vector <CALL >:: iterator itr = cache. begin (); itr! = Cache. end (); itr ++)
16 {
17 if (itr-> func ()){
18 run (itr-> name );
19}
20}
21}
 

But in this way, every time you add a funX, you need to write two lines of CALL structure initialization and add the function funX to the CLASS. For a lazy person like me, the amount of code still needs to be reduced by 75%. The advantages of lambda expressions are as follows:


1 # include <vector>
2 # include <functional>
3 using namespace std;
4
5 class
6 {
7 public:
8 struct CALL {
9 function <bool (void)> func;
10 string name;
11 };
12
13 ();
14 ~ A ();
15 void update ();
16 private:
17
18 vector <CALL> cache;
19
20 void run (const string & s );
21}

A: ()
{
CALL call [3];
Int I;
For (I = 0; I <3; ++ I)
{
Char str [10];
Sprintf (str, "fun % d", I + 1 );
Call [I]. name = string (str );
}
Call [0] = [] () {
Bool ret
...;
Return ret;
};
Call [1] = [] () {
Bool ret
...;
Return ret;
};
Call [2] = [] () {
Bool ret
...;
Return ret;
}
}

Void A: update ()
{
For (vector <CALl >:: iterator itr = cache. begin (); itr! = Cache. end (); itr ++)
{
If (itr-> func ()){
Run (itr-> name );
}
}
}
 

If you want to add any function, you only need to write it directly without inheriting the polymorphism.

Advantages: less code, code as a document, random addition and subtraction of products, no efficiency problems

 


Blog: http://www.cnblogs.com/cydonia/

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.