[問題] 請教一個語法問題
看了一個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)
推
03/02 10:11, , 1F
03/02 10:11, 1F
→
03/02 10:11, , 2F
03/02 10:11, 2F
→
03/02 10:11, , 3F
03/02 10:11, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章