C/C++

Как объявить двумерный динамический массив, и звполнить его с клавиатуры

Как объявить двумерный динамический массив, и звполнить его с клавиатуры
#include <iostream>
#include <iomanip>
using namespace std;
int* create_vector(const size_t n, const int value = 0) {
auto vec = new(nothrow) int[n];
if (vec != nullptr) {
for (auto i = 0U; i < n; ++i) {
vec[i] = value;
}
}
return vec;
}
int* destroy_vector(int* vec) {
if (vec != nullptr) {
delete[] vec;
vec = nullptr;
}
return vec;
}
int** create_matrix(const size_t n, const size_t m, const int value = 0) {
auto mtx = new(nothrow) int*[n];
if (mtx != nullptr) {
for (auto i = 0U; i < n; ++i) {
mtx[i] = create_vector(m, value);
if (mtx[i] == nullptr) {
for (auto j = 0U; j < i; ++j) {
mtx[j] = destroy_vector(mtx[j]);
}
delete[] mtx;
mtx = nullptr;
break;
}
}
}
return mtx;
}
int** destroy_matrix(int** mtx, const size_t n) {
if (mtx != nullptr) {
for (auto i = 0U; i < n; ++i) {
if (mtx[i] != nullptr) {
mtx[i] = destroy_vector(mtx[i]);
}
}
delete[] mtx;
mtx = nullptr;
}
return mtx;
}
void show(int** mtx, const size_t n, const size_t m, const streamsize w) {
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
cout << setw(w) << mtx[i][j];
}
puts("");
}
}
void fill(int** mtx, const size_t n, const size_t m) {
cout << "Input Matrix " << n << " x " << m << ":\n";
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
cin >> mtx[i][j];
}
}
system("cls");
}
int main() {
size_t n = 3;
size_t m = 5;
auto matrix = create_matrix(n, m);
if (matrix != nullptr) {
streamsize w = 4;
show(matrix, n, m, w);
fill(matrix, n, m);
show(matrix, n, m, w);
matrix = destroy_matrix(matrix, n);
}
system("pause > nul");
}
Павел Раковский
Павел Раковский
97 686
Лучший ответ
Турлыбек Кадыров пачему там слажно?
ну пусть есть ввод
MyClass* input();
тогда
MyClass*** arr = new MyClass**[ 42 ];
for( int i = 0; i < 42; ++i )
{
arr[i] = new MyClass*[42];
for( int j = 0; j < 42; ++j ) arr[ i ][ j ] = input();
}
WE
Wyacheslav Ewdokimov
38 458