Advertisement
avr39ripe

copyArrPointerTemplate

Dec 30th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. int copy(T* srcB, T* srcE, T* destB, T* destE)
  5. {
  6.     int copyCount{ 0 };
  7.  
  8.     while (destB != destE and srcB != srcE)
  9.     {
  10.         *destB++ = *srcB++;
  11.         ++copyCount;
  12.     }
  13.  
  14.     return copyCount;
  15. }
  16.  
  17. template <typename T>
  18. void print(T* begin, T* end)
  19. {
  20.     while (begin != end )
  21.     {
  22.         std::cout << *begin << ' ';
  23.         ++begin;
  24.     }
  25.     std::cout << '\n';
  26. }
  27.  
  28. int main()
  29. {
  30.     const int arrSize{10};
  31.     int arr[arrSize]{1,2,3,4,5,6,7,8,9,10};
  32.     int arr1[arrSize]{};
  33.  
  34.     char str[arrSize]{'a','b','c','d','e','f','g','h','i','j'};
  35.     char str1[arrSize]{};
  36.  
  37.     copy(arr,arr+arrSize,arr1,arr1+arrSize);
  38.     print(arr1,arr1+arrSize);
  39.  
  40.     copy(str,str+arrSize,str1,str1+arrSize);
  41.     print(str1,str1+arrSize);
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement