Reminder: in case of any technical issues, you can use the lightweight website m1.codeforces.com, m2.codeforces.com, m3.codeforces.com. ×

Dhruv_gheewala's blog

By Dhruv_gheewala, history, 3 weeks ago, In English,

What do these statement does? i can't understand the right-hand of =(equal to) does. can anyone help me with it??

lang : c++ Statement : auto Submask=[&](int A,int B{return((A&B)==A);};

Thanks in advance !!

 
 
 
 
  • Vote: I like it
  • -19
  • Vote: I do not like it

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

It's lambda expression. You can read about it here.

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Ok so they can be passed as a parameter to functions in c++ for example sort function. Like the codes below.

vector<int> v; // vector to be sorted
sort(v.begin(),v.end(),[](const int & a,const int & b){return a>b;} // sorts in descending order

if you feel uncomfortable with these type of expressions you can also do the same thing in another way

// outside main
bool comp(const int & a,const int & b){
    return a>b;
}
// inside main
vector<int> v;// vector to be sorted
sort(v.begin(),v.end(),comp);// does the same thing as above, sorts in descending order

It is up to you what feels easy to use. Cheers :)

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    but here we are given a '&' in bracket [] what is the use of it and what is the use of that bracket and Submask is function, functor or variable ?

    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      These are called lambda captures if you are interested more here. Cheers :)