Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.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++ 如何声明全局2d 3d 4d。。。可以在整个程序中使用的数组(堆版本)变量?_C++_Heap - Fatal编程技术网

C++ 如何声明全局2d 3d 4d。。。可以在整个程序中使用的数组(堆版本)变量?

C++ 如何声明全局2d 3d 4d。。。可以在整个程序中使用的数组(堆版本)变量?,c++,heap,C++,Heap,类别1.cpp int a=10; int b=5; int c=2; //for this array[a][b][c] int*** array=new int**[a]; for(int i =0; i<a; i++) { array[i] = new int*[b]; for(int k =0; k<b; k++) { array[i][k] = new int[c]; } } inta=10;in

类别1.cpp

int a=10; int b=5; int c=2;
//for this array[a][b][c]

int*** array=new int**[a];


for(int i =0; i<a; i++)
{ 
    array[i] = new int*[b];        
    for(int k =0; k<b; k++) 
    {
       array[i][k] = new int[c];
    }  
}
inta=10;int b=5;int c=2;
//对于此数组[a][b][c]
整数***数组=新整数**[a];

对于(int i=0;i而不是手动分配数组,您至少应该使用
std::vector
。您要做的是有一个包含

extern std::vector<std::vector<std::vector<int>>> data;
单个cpp文件将包含

matrix data(a, b, c);

您至少应该使用
std::vector
,而不是手动分配数组。您可以使用包含

extern std::vector<std::vector<std::vector<int>>> data;
单个cpp文件将包含

matrix data(a, b, c);

与原始数组相比,您更喜欢
std::array
std::vector
。您的维度是常量,因此请使用
std::array
。 在头文件中声明它:

// header.h
#pragma once  // or use multiple inclusion guards with preprocessor
#include <array>

const int a = 10;
const int b = 5;
const int c = 2;

using Array3D = std::array<std::array<std::array<int,c>,b>,a>;

extern Array3D array3d;  // extern indicates it is global
然后将标题包含在您想要使用它的任何位置

// main.cpp
#include "header.h"

int main()
{
    array3d[3][2][1] = 42; 
} 

与原始数组相比,您更喜欢
std::array
std::vector
。您的维度是常量,因此请使用
std::array
。 在头文件中声明它:

// header.h
#pragma once  // or use multiple inclusion guards with preprocessor
#include <array>

const int a = 10;
const int b = 5;
const int c = 2;

using Array3D = std::array<std::array<std::array<int,c>,b>,a>;

extern Array3D array3d;  // extern indicates it is global
然后将标题包含在您想要使用它的任何位置

// main.cpp
#include "header.h"

int main()
{
    array3d[3][2][1] = 42; 
} 

我不确定我是否理解了你的确切意思,只是:

class1 obj1;
obj1.array[i][j][k] // assuming you make the array public and already initialized in the constructor(and dont forget to delete it in the destructor)

我不确定我是否理解了你的确切意思,只是:

class1 obj1;
obj1.array[i][j][k] // assuming you make the array public and already initialized in the constructor(and dont forget to delete it in the destructor)

使用向量向量的向量…可能你不需要那么多维度。Oof-不要!使用一个好的1D向量并伪造索引…这里有一个例子:使用向量向量的向量…可能你不需要那么多维度。Oof-不要!使用一个好的1D向量并伪造索引…这里有一个例子:没有显示de>class1
已定义这没有多大帮助。如果不显示如何定义
class1
,这没有多大帮助。无法达成更多一致。性能需要连续向量。无法达成更多一致。性能需要连续向量。