Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. template<class T,const int N>
  2. T* createArr(T (*gen)() ){
  3.     T * mas = new T[N];
  4.     for (int i=0; i<N; i++){
  5.         mas[i] = gen();
  6.     }
  7.     return mas;
  8. }
  9.  
  10. //--------------------------
  11. template<class T>
  12. T gen(){
  13.     return rand()%100;
  14. }
  15.  
  16. int main(){
  17.     const int N = 10;
  18.     int * arr;
  19.     arr = createArr<int, N>(gen);
  20.     return 0;
  21. }
  22. //-------------------------------
  23.  
  24.  
  25.  
  26. template<class T, const int N>
  27. void map(T* arr, T (*change)(T) ){
  28.     for(int i=0; i<N; i++){
  29.         arr[i] = change(arr[i]);
  30.     }
  31. }
  32.  
  33. template<class T>
  34. T change(T elem){
  35.     return elem * elem;
  36. }
  37.  
  38. int main(){
  39.     const int N = 5;
  40.     int * arr = new int[N];
  41.     for (int i=0; i<5; i++){
  42.         arr[i] = i;
  43.     }
  44.     map<int, N>(arr, change);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement