fork download
  1. #include <iostream>
  2. #include <array>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. T operator+(const T& a1, const T& a2)
  8. {
  9. T a;
  10. for (typename T::size_type i = 0; i < a1.size(); i++)
  11. a[i] = a1[i] + a2[i];
  12. return a;
  13. }
  14.  
  15. int main()
  16. {
  17. array<int,5> a1 = { 1, 2, 3, 4, 5 };
  18. array<int,5> a2 = { 2, 3, 4, 5, 6 };
  19. array<int,5> a3 = a1 + a2;
  20.  
  21. for (int i = 0; i < 5; i++)
  22. cout << a1[i] << '+' << a2[i] << '=' << a3[i] << ' ';
  23.  
  24. cout << endl;
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
1+2=3 2+3=5 3+4=7 4+5=9 5+6=11