void process_z_vec(vector<int>& vec){auto print_2d = [](const vector<int>& board, int bsize){for(int i = 0; i<bsize; i++){for(int j=0; j<bsize; j++){cout << board[bsize*i+j] << " ";}cout << "\n";}};// Do sth with the vec.print_2d(vec,x_size);// Do sth else with the vec.print_2d(vec,y_size);//...}
void print_modulo(const vector<int>& v, ostream& os, int m) // output v[i] to os if v[i]%m==0{for_each(begin(v),end(v),[&os,m](int x) {if (x%m==0) os << x << '\n';});}
或通过功能
class Modulo_print {ostream& os; // members to hold the capture list int m;public:Modulo_print(ostream& s, int mm) :os(s), m(mm) {}void operator()(int x) const{if (x%m==0) os << x << '\n';}};
甚至
void print_modulo(const vector<int>& v, ostream& os, int m)// output v[i] to os if v[i]%m==0{class Modulo_print {ostream& os; // members to hold the capture listint m;public:Modulo_print (ostream& s, int mm) :os(s), m(mm) {}void operator()(int x) const{if (x%m==0) os << x << '\n';}};for_each(begin(v),end(v),Modulo_print{os,m});}
如果你需要,你可以像下面这样命名lambda expression:
void print_modulo(const vector<int>& v, ostream& os, int m)// output v[i] to os if v[i]%m==0{auto Modulo_print = [&os,m] (int x) { if (x%m==0) os << x << '\n'; };for_each(begin(v),end(v),Modulo_print);}
或者假设另一个简单的样本
void TestFunctions::simpleLambda() {bool sensitive = true;std::vector<int> v = std::vector<int>({1,33,3,4,5,6,7});
sort(v.begin(),v.end(),[sensitive](int x, int y) {printf("\n%i\n", x < y);return sensitive ? x < y : abs(x) < abs(y);});
printf("sorted");for_each(v.begin(), v.end(),[](int x) {printf("x - %i;", x);});}
int main(){// Lambda & autoint member=10;auto endGame = [=](int a, int b){ return a+b+member;};
endGame(4,5);
return 0;
}
编译如何扩展它:
int main(){int member = 10;
class __lambda_6_18{int member;public:inline /*constexpr */ int operator()(int a, int b) const{return a + b + member;}
public: __lambda_6_18(int _member): member{_member}{}
};
__lambda_6_18 endGame = __lambda_6_18{member};endGame.operator()(4, 5);
return 0;}
// C++ program to demonstrate lambda expression in C++#include <bits/stdc++.h>using namespace std;
// Function to print vectorvoid printVector(vector<int> v){// lambda expression to print vectorfor_each(v.begin(), v.end(), [](int i){std::cout << i << " ";});cout << endl;}
int main(){vector<int> v {4, 1, 3, 5, 2, 3, 1, 7};
printVector(v);
// below snippet find first number greater than 4// find_if searches for an element for which// function(third argument) returns truevector<int>:: iterator p = find_if(v.begin(), v.end(), [](int i){return i > 4;});cout << "First number greater than 4 is : " << *p << endl;
// function to sort vector, lambda expression is for sorting in// non-decreasing order Compiler can make out return type as// bool, but shown here just for explanationsort(v.begin(), v.end(), [](const int& a, const int& b) -> bool{return a > b;});
printVector(v);
// function to count numbers greater than or equal to 5int count_5 = count_if(v.begin(), v.end(), [](int a){return (a >= 5);});cout << "The number of elements greater than or equal to 5 is : "<< count_5 << endl;
// function for removing duplicate element (after sorting all// duplicate comes together)p = unique(v.begin(), v.end(), [](int a, int b){return a == b;});
// resizing vector to make size equal to total different numberv.resize(distance(v.begin(), p));printVector(v);
// accumulate function accumulate the container on the basis of// function provided as third argumentint arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int f = accumulate(arr, arr + 10, 1, [](int i, int j){return i * j;});
cout << "Factorial of 10 is : " << f << endl;
// We can also access function by storing this into variableauto square = [](int i){return i * i;};
cout << "Square of 5 is : " << square(5) << endl;}
产出
4 1 3 5 2 3 1 7First number greater than 4 is : 57 5 4 3 3 2 1 1The number of elements greater than or equal to 5 is : 27 5 4 3 2 1Factorial of 10 is : 3628800Square of 5 is : 25
#include <bits/stdc++.h>using namespace std;
int main(){vector<int> v1 = {3, 1, 7, 9};vector<int> v2 = {10, 2, 7, 16, 9};
// access v1 and v2 by referenceauto pushinto = [&] (int m){v1.push_back(m);v2.push_back(m);};
// it pushes 20 in both v1 and v2pushinto(20);
// access v1 by copy[v1](){for (auto p = v1.begin(); p != v1.end(); p++){cout << *p << " ";}};
int N = 5;
// below snippet find first number greater than N// [N] denotes, can access only N by valuevector<int>:: iterator p = find_if(v1.begin(), v1.end(), [N](int i){return i > N;});
cout << "First number greater than 5 is : " << *p << endl;
// function to count numbers greater than or equal to N// [=] denotes, can access all variableint count_N = count_if(v1.begin(), v1.end(), [=](int a){return (a >= N);});
cout << "The number of elements greater than or equal to 5 is : "<< count_N << endl;}
输出:
First number greater than 5 is : 7The number of elements greater than or equal to 5 is : 3