Advertisement
Guest User

Untitled

a guest
May 28th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. //temp.h
  2. #ifndef TEMP_H
  3. #define TEMP_H
  4.  
  5. template <class T>
  6. class temp
  7. {
  8. int size;
  9. T* arr;
  10.  
  11. public:
  12. temp(int i);
  13. void add(T ffs);
  14. T at(int i);
  15. T remove(int i);
  16. T length();
  17. };
  18.  
  19.  
  20. #endif // TEMP_H
  21.  
  22. //main.cpp
  23.  
  24. #include <iostream>
  25. #include"temp.h"
  26. using namespace std;
  27. template<typename H>
  28. H find(H one, H two,H three )
  29. {
  30. return (one + two + three)/3;
  31. }
  32. int main()
  33. {
  34. temp <int> Arrays(5);
  35. return 0;
  36. }
  37.  
  38. //temp.cpp
  39.  
  40. #include "temp.h"
  41. #include<iostream>
  42. template <class T>
  43. temp<T>::temp(int i)
  44. {
  45. size = i;
  46. arr = new T[size];
  47. }
  48. template <class T>
  49. void temp<T>::add(T ffs)
  50. {
  51. T array= new T[size];
  52. for(int i=0;i<size;i++)
  53. {
  54. array[i]=arr[i];
  55. }
  56. delete[]arr;
  57. size++;
  58. arr = new T[size];
  59. for(int i=0; i<size; i++)
  60. {
  61. arr[i]=array[i];
  62. }
  63. delete[]array;
  64. arr[size-1] = ffs;
  65. }
  66. template <class T>
  67. T temp<T>::at(int i)
  68. {
  69. std::cout<<"запрашиваемый элемент "<<i<<" равен "<< arr[i];
  70. }
  71. template <class T>
  72. T temp<T>::remove(int i)//данный способ нерационален для массивов с большим количеством данных, но кого волнует...
  73. {
  74. T tempp;
  75. for(int j = i; j<size-1; j++)
  76. {
  77. tempp = arr[j];
  78. arr[j]=arr[j+1];
  79. arr[j+1]=tempp;
  80. }
  81. size--;
  82. T array = new T[size];
  83. for(int j=0;j<size;j++)
  84. {
  85. array[j]=arr[j];
  86. }
  87. return array;
  88. }
  89. template <class T>
  90. T temp<T>::length()
  91. {
  92.  
  93. return size;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement