fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct A{
  6. int a;
  7. A(): a(0){}
  8.  
  9. void operator()(int i) {
  10. if(i) a++;
  11. std::cout << "a:" << a << " @" << &a << std::endl;
  12. }
  13. };
  14.  
  15. int main(int argc, char *argv[]) {
  16. //test data
  17. std::vector<int> vec;
  18. vec.push_back(1);
  19. vec.push_back(1);
  20. vec.push_back(0);
  21.  
  22. //accumulator
  23. A accum;
  24.  
  25. std::for_each(vec.begin(), vec.end(), accum);
  26. std::cout << "non-zero elements:" << accum.a << " @" << &accum.a << std::endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
a:1  @0xbf86902c
a:2  @0xbf86902c
a:2  @0xbf86902c
non-zero elements:0  @0xbf869050