fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<typename Iter, typename Func>
  5. void combine_pairwise(Iter first, Iter last, Func func)
  6. {
  7. for(; first != last; ++first)
  8. for(Iter next = std::next(first); next != last; ++next)
  9. func(*first, *next);
  10. }
  11.  
  12. int main() {
  13. std::vector<float> v {5120.2, 5140.2, 5500.2, 5482.0, 5802.2};
  14. std::vector<float> result;
  15.  
  16. auto op = [&](const float&f1, const float&f2){result.push_back(f1-f2);};
  17. combine_pairwise(v.begin(), v.end(), op);
  18.  
  19. for (auto f : result)
  20. {
  21. std::cout << f << std::endl;
  22. }
  23. return 0;
  24. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
-20
-380
-361.8
-682
-360
-341.8
-662
18.2002
-302
-320.2