fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <typename T>
  5. int operator+(std::vector<T> v1, std::vector<T> v2) {
  6. if(v1.size() != v2.size()) { throw; } //for simplicity
  7. int sum = 0;
  8. for(size_t x = 0; x < v1.size(); x++) {
  9. sum += v1.at(x) + v2.at(x);
  10. }
  11. return sum;
  12. }
  13.  
  14. int main() {
  15. std::vector<int> v1(3, 5), v2(3, 1);
  16. std::cout << v1 + v2 << std::endl; //should be 5*3 + 3*1 = 18
  17.  
  18. std::vector<std::vector<int>> v3(2, std::vector<int>(3, 5)), v4(2, std::vector<int>(3, 1));
  19. std::cout << v3 + v4 << std::endl; //should be 5*3*2 + 3*1*2 = 36
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
18
36