fork download
  1. template<class iterator1, class iterator2>
  2. bool almostEqual(iterator1 begin1, iterator1 end1, iterator2 begin2, iterator2 end2, int tolerance)
  3. {
  4. for(;begin1!=end1; ++begin1, ++begin2) {
  5. if (begin2 == end2)
  6. return false;
  7. if (*begin1 - tolerance > *begin2)
  8. return false;
  9. if (*begin1 + tolerance < *begin2)
  10. return false;
  11. }
  12. if (begin2 != end2)
  13. return false;
  14. return true;
  15. }
  16.  
  17. template<class container1, class container2>
  18. bool almostEqual(container1 left, container2 right, int tolerance)
  19. { return almostEqual(left.begin(), left.end(), right.begin(), right.end(), tolerance);}
  20.  
  21. #include <vector>
  22. #include <iostream>
  23.  
  24. int main() {
  25. std::vector<int> a = {0, 5, 10};
  26. std::vector<int> b = {1, 4, 8};
  27. std::vector<int> c = {2, 2, 10};
  28.  
  29. std::cout << almostEqual(a, b, 2) << '\n';
  30. std::cout << almostEqual(a, c, 2) << '\n';
  31. std::cout << almostEqual(b, c, 2) << '\n';
  32. }
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
1
0
1