Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中创建给定大小的二维数组+;_C++_Arrays - Fatal编程技术网

C++ 如何在C+中创建给定大小的二维数组+;

C++ 如何在C+中创建给定大小的二维数组+;,c++,arrays,C++,Arrays,我需要创建一个给定大小的正方形矩阵。我知道如何创建给定大小的动态一维数组。对于像下面几行这样的二维数组,不也是这样吗 cin>>size; int* a[][]=new int[size][size] 您可以使用std::vector: std::vector<std::vector<int*>> a(size, std::vector<int*>(size)); 注意,C++中没有新[][]/Cuth>运算符,只需调用 NeX[]/COD>两

我需要创建一个给定大小的正方形矩阵。我知道如何创建给定大小的动态一维数组。对于像下面几行这样的二维数组,不也是这样吗

cin>>size;
int* a[][]=new int[size][size]

您可以使用
std::vector

std::vector<std::vector<int*>> a(size, std::vector<int*>(size));

注意,C++中没有<代码>新[][]/Cuth>运算符,只需调用<代码> NeX[]/COD>两次。< /P> 但是,如果要使用

new
delete
而不是
std::vector
,则应使用智能指针而不是原始指针,例如:

std::unique_ptr<std::unique_ptr<int*>[]> a(new std::unique_ptr<int*>[size]);
for (size_t i = 0; i < size; ++i)
    a[i].reset(new int*[size]);
...
// No need to call `delete`, std::unique_ptr does it automatically.
std::unique_ptr a(新std::unique_ptr[size]);
对于(大小i=0;i
您可以使用
std::vector

std::vector<std::vector<int*>> a(size, std::vector<int*>(size));

注意,C++中没有<代码>新[][]/Cuth>运算符,只需调用<代码> NeX[]/COD>两次。< /P> 但是,如果要使用

new
delete
而不是
std::vector
,则应使用智能指针而不是原始指针,例如:

std::unique_ptr<std::unique_ptr<int*>[]> a(new std::unique_ptr<int*>[size]);
for (size_t i = 0; i < size; ++i)
    a[i].reset(new int*[size]);
...
// No need to call `delete`, std::unique_ptr does it automatically.
std::unique_ptr a(新std::unique_ptr[size]);
对于(大小i=0;i
您可以使用
std::vector

std::vector<std::vector<int*>> a(size, std::vector<int*>(size));

注意,C++中没有<代码>新[][]/Cuth>运算符,只需调用<代码> NeX[]/COD>两次。< /P> 但是,如果要使用

new
delete
而不是
std::vector
,则应使用智能指针而不是原始指针,例如:

std::unique_ptr<std::unique_ptr<int*>[]> a(new std::unique_ptr<int*>[size]);
for (size_t i = 0; i < size; ++i)
    a[i].reset(new int*[size]);
...
// No need to call `delete`, std::unique_ptr does it automatically.
std::unique_ptr a(新std::unique_ptr[size]);
对于(大小i=0;i
您可以使用
std::vector

std::vector<std::vector<int*>> a(size, std::vector<int*>(size));

注意,C++中没有<代码>新[][]/Cuth>运算符,只需调用<代码> NeX[]/COD>两次。< /P> 但是,如果要使用

new
delete
而不是
std::vector
,则应使用智能指针而不是原始指针,例如:

std::unique_ptr<std::unique_ptr<int*>[]> a(new std::unique_ptr<int*>[size]);
for (size_t i = 0; i < size; ++i)
    a[i].reset(new int*[size]);
...
// No need to call `delete`, std::unique_ptr does it automatically.
std::unique_ptr a(新std::unique_ptr[size]);
对于(大小i=0;i
如果您使用的是C++11,那么也可以使用std::array

const int iRows = 3, iCols = 3; // number of rows and columns
std::array<std::array<int, iCols>, iRows> matrix;

// fill with 1,2,3  4,5,6  7,8,9
for(int i=0;i<iRows;++i)
    for(int j=0;j<iCols;++j)
        matrix[i][j] = i * iCols + j + 1;
如果数组对象是const限定的,则返回一个const引用;如果数组对象不是const限定的,则返回一个引用。请注意

std::array
不是大小可变的数组类型,如

std::vector

如果您使用的是C++11,那么还可以使用std::array

const int iRows = 3, iCols = 3; // number of rows and columns
std::array<std::array<int, iCols>, iRows> matrix;

// fill with 1,2,3  4,5,6  7,8,9
for(int i=0;i<iRows;++i)
    for(int j=0;j<iCols;++j)
        matrix[i][j] = i * iCols + j + 1;
如果数组对象是const限定的,则返回一个const引用;如果数组对象不是const限定的,则返回一个引用。请注意

std::array
不是大小可变的数组类型,如

std::vector

如果您使用的是C++11,那么还可以使用std::array

const int iRows = 3, iCols = 3; // number of rows and columns
std::array<std::array<int, iCols>, iRows> matrix;

// fill with 1,2,3  4,5,6  7,8,9
for(int i=0;i<iRows;++i)
    for(int j=0;j<iCols;++j)
        matrix[i][j] = i * iCols + j + 1;
如果数组对象是const限定的,则返回一个const引用;如果数组对象不是const限定的,则返回一个引用。请注意

std::array
不是大小可变的数组类型,如

std::vector

如果您使用的是C++11,那么还可以使用std::array

const int iRows = 3, iCols = 3; // number of rows and columns
std::array<std::array<int, iCols>, iRows> matrix;

// fill with 1,2,3  4,5,6  7,8,9
for(int i=0;i<iRows;++i)
    for(int j=0;j<iCols;++j)
        matrix[i][j] = i * iCols + j + 1;
如果数组对象是const限定的,则返回一个const引用;如果数组对象不是const限定的,则返回一个引用。请注意

std::array
不是大小可变的数组类型,如

std::vector
不,这不行

main.cpp:4: error: only the first dimension of an allocated array may have dynamic size
    new int[size][size];
                  ^~~~
如果行的大小是固定的,则可以执行以下操作:

// allocate an array with `size` rows and 10 columns
int (*array)[10] = new int[size][10];
<>在C++中,不能有二维维度的原始数组,其中两个维度都是动态的。这是因为原始数组索引根据指针工作;例如,为了访问第二行,指向第一行的指针需要按行的大小递增。但是当行的大小是动态的时,数组不知道大小,因此C++不知道如何计算指针增量。 如果您想要一个具有多个动态维度的数组,那么您需要构造数组分配,以便C++的默认数组索引逻辑能够处理它(例如重复问题的顶部答案),或者您需要自己实现逻辑来计算适当的指针增量

对于每行大小相同的数组,我建议不要使用多个分配,如答案所示,或使用向量向量。使用向量向量解决了手工分配的困难和危险性,但它仍然使用了超过必要的内存,并且不允许更快的内存访问模式

另一种方法是将多维数组展平,它可以使代码像其他方法一样易于读写,不需要额外的内存,并且可以执行得更好

展平数组意味着您仅使用与所需二维数组具有相同元素数的一维数组,并执行在多维索引和相应的一维索引之间转换的算法。使用
new
时,它看起来像:

int *arr = new int[row_count * column_count];
二维数组中的行
i
,列
j
对应于
arr[column\u count*i+j]
arr[n]
对应于行
n/column\u count
和列
n%column\u count
处的元素。例如,在具有10列的数组中,第0行第0列对应于
arr[0]
;第0行第1列对应于
arr[1]
;第1行第0列对应于
arr[10]
;第1行第1列对应于
arr[11]


您应该避免使用原始
new
delete
进行手动内存管理,例如在
int*arr=newint[size]的情况下。相反,资源管理应该封装在类中。一个用于动态管理