看板 C_and_CPP 關於我們 聯絡資訊
看了一個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