fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. vector<int> v = {1, 2, 3, 4, 5};
  9.  
  10. // Define an external variable
  11. int sum = 10;
  12.  
  13. // Pass the external variable to the lambda function
  14. for_each(v.begin(), v.end(), [sum](int i) {
  15. if (sum == 10)
  16. cout<<"shiv"<<endl;
  17. //sum += i;
  18. });
  19.  
  20. // Print the sum
  21. cout << sum << endl;
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
shiv
shiv
shiv
shiv
shiv
10