1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <iostream> #include <vector>
void iniarr(int** arr, int m, int n) { for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) arr[i][j] = i + j; }
void printarr(int** arr, int m, int n) { for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } std::cout << std::endl; }
void usearr(int** arr, int m, int n) { iniarr(arr, m, n); printarr(arr, m, n); }
int** mularr_ma(int m, int n) { int **arr = (int**)malloc(m * sizeof(int*)); for (int i = 0; i < m; ++i) arr[i] = (int*)malloc(n * sizeof(int)); return arr; }
int** mularr_ma2(int m, int n) { int **arr = (int**)malloc(m * sizeof(int*)); arr[0] = (int*)malloc(m * n * sizeof(int)); for (int i = 1; i < m; ++i) arr[i] = arr[i-1] + n; return arr; }
int** mularr_new(int m, int n) { int **arr = new int*[m]; for (int i = 0; i < m; ++i) arr[i] = new int[n]; return arr; }
int** mularr_new2(int m, int n) { int **arr = new int*[m]; arr[0] = new int[m * n]; for (int i = 1; i < m; ++i) arr[i] = arr[i - 1] + n; return arr; }
std::vector<std::vector<int>> mulvec(int m, int n) { std::vector<std::vector<int>> res(m, std::vector<int>(n)); return res; }
void usevec(std::vector<std::vector<int>> &vec) { for (int i = 0; i < vec.size(); ++i) for (int j = 0; j < (vec[0]).size(); ++j) vec[i][j] = i + j;
for (auto i : vec) { for (auto j : i) std::cout << j << " "; std::cout << std::endl; } std::cout << std::endl; }
int main(void) { int m = 5, n = 6; int **arr = mularr_ma(m, n); usearr(arr, m, n); for (int i = 0; i < m; ++i) free(arr[i]); free(arr);
arr = mularr_ma2(m, n); usearr(arr, m, n); free(arr[0]); free(arr);
arr = mularr_new(m, n); usearr(arr, m, n); for (int i = 0; i < m; ++i) delete[] arr[i]; delete[] arr;
arr = mularr_new2(m, n); usearr(arr, m, n); delete[] arr[0]; delete[] arr;
std::vector<std::vector<int>> varr = mulvec(m, n); usevec(varr); return 0; }
|