fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template<typename T>
  5.  
  6.  
  7. template<typename T>
  8. std::vector<T> slice(std::vector<T> const &v, int m, int n)
  9. {
  10. auto first = v.cbegin() + m;
  11. auto last = v.cbegin() + n + 1;
  12.  
  13. std::vector<T> vec(first, last);
  14. return vec;
  15. }
  16.  
  17. int main()
  18. {
  19. // input vector
  20. std::vector<int> v = { 1, 2, 3, 4, 2, 2, 4, 6, 5 };
  21.  
  22. vector<int>::iterator ptr;
  23. // starting and ending index
  24. int m = 4, n = 7;
  25.  
  26. std::vector<int> sub_vec = slice(v, m, n);
  27. ptr = sub_vec.begin();
  28. cout<<&(*ptr)<<" ";
  29. ptr = v.begin()+4;
  30. //print(sub_vec);
  31. cout<<&(*ptr)<<" ";
  32. ptr = v.begin()+4;
  33.  
  34. return 0;
  35. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:10: error: declaration of template parameter ‘T’ shadows template parameter
 template<typename T>
          ^~~~~~~~
prog.cpp:4:10: note: template parameter ‘T’ declared here
 template<typename T>
          ^~~~~~~~
prog.cpp:8:16: error: too many template-parameter-lists
 std::vector<T> slice(std::vector<T> const &v, int m, int n)
                ^~~~~
prog.cpp: In function ‘int main()’:
prog.cpp:26:45: error: ‘slice’ was not declared in this scope
     std::vector<int> sub_vec = slice(v, m, n);
                                             ^
stdout
Standard output is empty