????Lambda????????????????void
????????????????ú?????????????????????
#include<iostream>
#include<algorithm>
#include<vector>
#include<ostream>
#include<cassert>
using namespace std;
class LambdaFunctor{
public:
void operator()(int n) const{
cout << n << " ";
}
};
int main()
{
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
for_each(v.begin()?? v.end()?? LambdaFunctor());
cout << endl;
return 0;
}
?????????£????????Lambda??????????
????????????Lambda????????????????????????????????????????“????”?????????????????????[]???г?????????“??”?????б????????????????????????????Щ???????ο?????????
#include<iostream>
#include<algorithm>
#include<vector>
#include<ostream>
#include<cassert>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
//???for_each????Lambda??????????????????
int evenCount = 0;
for_each(v.begin()?? v.end()?? [&evenCount](int n){
cout << n;
if (n % 2 == 0) {
cout << " is even" << endl;
evenCount++;
}
else {
cout << " is odd" << endl;
}
});
cout << "There are " << evenCount << " even numbers in the vector." << endl;
getchar();
return 0;
}