// Function objects overload operator() so they act like functionsdoublenumber=add()(2,3);std::cout<<number<<'\n';
12
// Automatically creates a functor for the functionautofunction=[](){std::cout<<"Hello, world! ";};
1
function();
1234
// Sort in increasing orderstd::vector<int>v{4,3,1,2};autocomparison=[](inta,intb){returna<b;};std::sort(v.begin(),v.end(),comparison);
123456
// Sort in decreasing orderstd::sort(v.begin(),v.end(),[](inta,intb){returna>b;});// Print each elementstd::for_each(v.begin(),v.end(),[](constdoublec){std::cout<<c<<" ";});std::cout<<'\n';
// These values are external to the function parametersintx=10;std::function<int(int)>func2=[x](inti){returni+x;};std::cout<<"func2(6): "<<func2(6)<<'\n';
// Creates new functions from existing functions// Create base function my_divideautomy_divide=[](doublex,doubley){returnx/y;};std::cout<<"my_divide(4,7): "<<my_divide(4,7)<<'\n';// Make x and y always 10 and 2// Function fn_five has no parameters nowautofn_five=std::bind(my_divide,10,2);std::cout<<"fn_five(): "<<fn_five()<<'\n';
1234
// Function has only one parameter nowautofn_half=std::bind(my_divide,std::placeholders::_1,2);// returns x/2std::cout<<"fn_half(10): "<<fn_half(10)<<'\n';
std::vector<int>myvector={32,71,12,45,26,80,53,33};std::sort(myvector.begin(),myvector.begin()+4);// uses operator <std::sort(myvector.begin()+4,myvector.end(),[](autox,autoy){returnx>y;});// use operator >
123456
// Many algorithms hardly make much sense without lambdasstd::vector<int>foo={3,5,7,11,13,17,19,23};autois_odd=[](inti){returni%2;};if(std::all_of(foo.begin(),foo.end(),is_odd)){std::cout<<"All the elements are odd numbers.\n";}
1234
std::array<int,7>foo2={0,1,-1,3,-3,5,-5};if(std::any_of(foo2.begin(),foo2.end(),[](inti){returni<0;})){std::cout<<"There are negative elements in the range.\n";}
1234
std::array<int,8>foo3={1,2,4,8,16,32,64,128};if(std::none_of(foo3.begin(),foo3.end(),[](inti){returni<0;})){std::cout<<"There are no negative elements in the range.\n";}
1234
std::vector<int>v3={10,25,40,55};autoit=std::find_if(v3.begin(),v3.end(),[](autoi){returni%2==1;});std::cout<<"The first odd value is "<<*it<<'\n';