作者FAITHY ()
看板C_and_CPP
標題[問題] 請教一個語法問題
時間Wed Mar 2 10:05:17 2011
看了一個lambda的範例code 看到一段的
不是很懂這是這是什麼語意(標示黃字部份...)
請問這語法是在表示什麼呢
感謝:)
Copy // even_lambda.cpp
// compile with: /EHsc
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Create a vector object that contains 10 elements.
vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i);
}
// Count the number of even numbers in the vector by
// using the for_each function and a lambda expression.
int evenCount = 0;
for_each(v.begin(), v.end(),
[&evenCount] (int n) {
cout << n;
if (n % 2 == 0)
{
cout << " is even " << endl;
// Increment the counter.
evenCount++;
}
else
{
cout << " is odd " << endl;
}
});
// Print the count of even numbers to the console.
cout << "There are " << evenCount
<< " even numbers in the vector." << endl;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
※ 編輯: FAITHY 來自: 219.87.71.10 (03/02 10:05)
推 scwg:code sample 下面的 comments 裡就有寫了啊... 03/02 10:11
→ scwg:The [&evenCount] part specifies the capture clause of 03/02 10:11
→ scwg:the expression 03/02 10:11