Advertisement
awsmpshk

Untitled

Mar 12th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. //№2 на семинар по информатике и программированию. Создать двумерный массив при помощи template<>:
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. T** createMas(const int& n) {
  8. T** mas = new T * [n];
  9. for (int i = 0; i < n; ++i) {
  10. mas[i] = new T[n];
  11. for (int j = 0; j < n; ++j) {
  12. cin >> a[i][j];
  13. }
  14. }
  15. return mas;
  16. }
  17.  
  18. template <typename T>
  19. void printMas(T** mas, const int& n) {
  20. for (int i = 0; i < n; ++i) {
  21. for (int j = 0; j < n; ++j) {
  22. cout << a[i][j] << " ";
  23. }
  24. cout << endl;
  25. }
  26. }
  27.  
  28. template <typename T>
  29. void deleteMas(T** mas, const int& n) {
  30. for (int i = 0; i < n; ++i) {
  31. delete[] mas[i];
  32. }
  33. delete[] mas;
  34. }
  35.  
  36. int main() {
  37. int n;
  38. cin >> n;
  39. int** a = createMas<int>(n);
  40. printMas(a, n);
  41. deleteMas(a, n);
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement