[問題] 請教一個語法問題

看板C_and_CPP (C/C++)作者時間15年前 (2011/03/02 10:05), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串1/1
看了一個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
code sample 下面的 comments 裡就有寫了啊...
03/02 10:11, 1F

03/02 10:11, , 2F
The [&evenCount] part specifies the capture clause of
03/02 10:11, 2F

03/02 10:11, , 3F
the expression
03/02 10:11, 3F
文章代碼(AID): #1DRQNWL2 (C_and_CPP)
文章代碼(AID): #1DRQNWL2 (C_and_CPP)