| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- // CallbackFunctions.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
- //
- #include <iostream>
- #include <vector>
- using namespace std;
- void print(vector<int> v) {
- for (auto x : v) cout << x << " ";
- cout << endl;
- }
- /*
- []: can only access variables which are local to it.
- [&]: capture all external variables by reference.
- [=]: capture all external variables by value.
- [a, &b]: capture ‘a’ by value and ‘b’ by reference.
- */
- int main() {
- vector<int> v1, v2;
- // Capture v1 and v2 by reference
- auto byRef = [&](int m) {
- v1.push_back(m);
- v2.push_back(m);
- };
- // Capture v1 and v2 by value
- auto byVal = [=](int m) mutable {
- v1.push_back(m);
- v2.push_back(m);
- };
- // Capture v1 by reference and v2 by value
- auto mixed = [=, &v1](int m) mutable {
- v1.push_back(m);
- v2.push_back(m);
- };
- // Push 20 in both v1 and v2
- byRef(20);
- // Push 234 in both v1 and v2
- byVal(234);
- // Push 10 in both v1 and v2
- mixed(10);
- print(v1);
- print(v2);
- return 0;
- }
|