#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
const long Size = 390000L;
int main()
{
vector<int> numbers(Size);
srand(time(0));
generate(numbers.begin(), numbers.end(), rand);
cout << "Sample size=" << Size << endl;
//using lambdas
int count3 = count_if(numbers.begin(), numbers.end(), [](int x) {return x % 3 == 0; });
cout << "Count of numbers divisible by 3:" << count3 << endl;
//using a single lambda
int count13 = 0;//可修改自動變量的lambda表達式
for_each(numbers.begin(), numbers.end(), [&count13](int x) {count13 += x % 13 == 0; });
cout << "Count of numbers divisible by 13 is:" << count13 << endl;
//using a single lambda
count3 = count13 = 0;
for_each(numbers.begin(), numbers.end(), [&](int x) {count3 += x % 3 == 0; count13 += x % 13 == 0; });
cout << "Count of numbers divisible by 3:" << count3 << endl;
cout << "Count of numbers divisible by 13 is:" << count13 << endl;
system("pause");
return 0;
}
002.含參數lambda表達式使用實例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.