fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. void foo(int &n) {
  7. n++;
  8. }
  9.  
  10. int main() {
  11. std::vector<int> nums{ 3, 4, 2, 9, 15, 267 };
  12.  
  13. std::cout << "Before: ";
  14. for (auto n : nums) {
  15. std::cout << n << " ";
  16. }
  17. std::cout << '\n';
  18.  
  19. std::for_each(nums.begin(), nums.end(), foo);
  20.  
  21. std::cout << "After: ";
  22. for (auto n : nums) {
  23. std::cout << n << " ";
  24. }
  25. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Before: 3 4 2 9 15 267 
After:  4 5 3 10 16 268