如何使用new声明2D数组?
就像,对于“普通”数组,我将:
int* ary = new int[Size]
但
int** ary = new int[sizeY][sizeX]
a)无法工作/编译,b)无法完成以下任务:
int ary[sizeY][sizeX] 
做。
#1楼
在C ++ 11中,可能:
auto array = new double[M][N]; 
这样,内存不会被初始化。 要初始化它,请执行以下操作:
auto array = new double[M][N]();
示例程序(使用“ g ++ -std = c ++ 11”编译):
#include <iostream>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
int main()
{
    const auto M = 2;
    const auto N = 2;
    // allocate (no initializatoin)
    auto array = new double[M][N];
    // pollute the memory
    array[0][0] = 2;
    array[1][0] = 3;
    array[0][1] = 4;
    array[1][1] = 5;
    // re-allocate, probably will fetch the same memory block (not portable)
    delete[] array;
    array = new double[M][N];
    // show that memory is not initialized
    for(int r = 0; r < M; r++)
    {
        for(int c = 0; c < N; c++)
            cout << array[r][c] << " ";
        cout << endl;
    }
    cout << endl;
    delete[] array;
    // the proper way to zero-initialize the array
    array = new double[M][N]();
    // show the memory is initialized
    for(int r = 0; r < M; r++)
    {
        for(int c = 0; c < N; c++)
            cout << array[r][c] << " ";
        cout << endl;
    }
    int info;
    cout << abi::__cxa_demangle(typeid(array).name(),0,0,&info) << endl;
    return 0;
}
输出:
2 4 
3 5 
0 0 
0 0 
double (*) [2]
#2楼
动态声明2D数组:
    #include<iostream>
    using namespace std;
    int main()
    {
        int x = 3, y = 3;
        int **ptr = new int *[x];
        for(int i = 0; i<y; i++)
        {
            ptr[i] = new int[y];
        }
        srand(time(0));
        for(int j = 0; j<x; j++)
        {
            for(int k = 0; k<y; k++)
            {
                int a = rand()%10;
                ptr[j][k] = a;
                cout<<ptr[j][k]<<" ";
            }
            cout<<endl;
        }
    }
现在,在上面的代码中,我们使用了一个双指针,并为其分配了一个动态内存,并给出了列的值。 这里分配的内存仅用于列,现在对于行,我们只需要一个for循环,并为每行的值分配一个动态内存。 现在,我们可以像使用2D数组一样使用指针。 在上面的示例中,我们然后为2D数组(指针)分配了随机数,所有这些都与2D数组的DMA有关。
#3楼
在某些情况下,我为您提供了最适合我的解决方案。 尤其是如果知道[?的大小]数组的一维。 对于char数组非常有用,例如,如果我们需要大小可变的char [20]数组。
int  size = 1492;
char (*array)[20];
array = new char[size][20];
...
strcpy(array[5], "hola!");
...
delete [] array;
关键是数组声明中的括号。
#4楼
如果您的项目是CLI(公共语言运行时支持) ,则:
您可以使用数组类,而不是在编写时得到的那个:
#include <array>
using namespace std;
换句话说,不是在使用std名称空间时以及在包含数组头时获得的非托管数组类,不是在std名称空间和数组头中定义的非托管数组类,而是CLI的托管类数组。
使用此类,您可以创建任意等级的数组。
下面的代码创建一个新的二维数组,该数组包含2行3列,类型为int,我将其命名为“ arr”:
array<int, 2>^ arr = gcnew array<int, 2>(2, 3);
 现在,您可以访问元素的数组中,按名称,并只写一个平方括号[]并在他们里面,添加的行和列,并将它们与逗号分隔, 。 
下面的以下代码访问我在上面的先前代码中已经创建的数组的第二行和第一列中的元素:
arr[0, 1]
 仅写此行是为了读取该单元格中的值,即获取该单元格中的值,但是如果添加等于=符号,则将要在该单元格中写入值,即在该单元格中设置值。 当然,您也可以使用+ =,-=,* =和/ =运算符,仅用于数字(int,float,double,__ int16,__ int32,__ int64等),但请确保您已经知道。 
 如果您的项目不是 CLI,则当然可以使用std名称空间的非托管数组类,当然,如果您#include <array> ,则问题在于此数组类与CLI数组不同。 创建此类型的数组与CLI相同,只不过您必须删除^符号和gcnew关键字。 但是不幸的是, <>括号中的第二个int参数指定了数组的长度(即大小) , 而不是其等级! 
在这种阵列中无法指定等级,等级仅是CLI阵列的功能。 。
 std数组的行为类似于c ++中的普通数组,您可以使用指针来定义,例如int*然后定义: new int[size]或不使用指针: int arr[size] ,但是与c ++的普通数组不同,std数组提供了可以与数组元素一起使用的函数,例如填充,开始,结束,大小等,但是普通数组什么也没提供。 
但是std数组仍然是一维数组,就像普通的c ++数组一样。 但是,由于其他人提出了有关如何将普通的c ++一维数组转换为二维数组的解决方案,我们可以将相同的想法应用于std数组,例如,根据Mehrdad Afshari的想法,我们可以编写以下代码:
array<array<int, 3>, 2> array2d = array<array<int, 3>, 2>();
此行代码创建一个“伪数组” ,它是一个一维数组,它的每个单元格都是或指向另一个一维数组。
如果一维数组中的所有一维数组的长度/大小均相等,则可以将array2d变量视为真正的二维数组,此外,还可以使用特殊方法来处理行或列,具体取决于查看方式请记住,在2D数组中,该std数组支持。
您还可以使用Kevin Loney的解决方案:
int *ary = new int[sizeX*sizeY];
// ary[i][j] is then rewritten as
ary[i*sizeY+j]
但是如果使用std数组,则代码必须看起来不同:
array<int, sizeX*sizeY> ary = array<int, sizeX*sizeY>();
ary.at(i*sizeY+j);
并且仍然具有std数组的独特功能。
 注意,您仍然可以使用[]括号来访问std数组的元素,而不必调用at函数。 您还可以定义并分配新的int变量,该变量将计算并保留std数组中的元素总数,并使用其值,而不是重复sizeX*sizeY 
您可以定义自己的二维数组泛型类,定义二维数组类的构造函数以接收两个整数以指定新二维数组中的行数和列数,并定义get函数以接收两个整数参数会访问二维数组中的元素并返回其值,并设置函数以接收三个参数,其中第一个是整数,它们指定二维数组中的行和列,第三个参数是元件。 它的类型取决于您在通用类中选择的类型。
您将能够通过使用普通的C ++数组(指针或无) 或想法,其他人提出的STD阵列和使用一个实现这一切,并可以很容易地使用像CLI数组,或像两您可以在C#中定义,分配和使用的三维数组。
#5楼
这个问题困扰了我15年,所有提供的解决方案都不令我满意。 如何在内存中连续创建动态多维数组? 今天我终于找到了答案。 使用以下代码,您可以做到这一点:
#include <iostream>
int main(int argc, char** argv)
{
    if (argc != 3)
    {
        std::cerr << "You have to specify the two array dimensions" << std::endl;
        return -1;
    }
    int sizeX, sizeY;
    sizeX = std::stoi(argv[1]);
    sizeY = std::stoi(argv[2]);
    if (sizeX <= 0)
    {
        std::cerr << "Invalid dimension x" << std::endl;
        return -1;
    }
    if (sizeY <= 0)
    {
        std::cerr << "Invalid dimension y" << std::endl;
        return -1;
    }
    /******** Create a two dimensional dynamic array in continuous memory ******
     *
     * - Define the pointer holding the array
     * - Allocate memory for the array (linear)
     * - Allocate memory for the pointers inside the array
     * - Assign the pointers inside the array the corresponding addresses
     *   in the linear array
     **************************************************************************/
    // The resulting array
    unsigned int** array2d;
    // Linear memory allocation
    unsigned int* temp = new unsigned int[sizeX * sizeY];
    // These are the important steps:
    // Allocate the pointers inside the array,
    // which will be used to index the linear memory
    array2d = new unsigned int*[sizeY];
    // Let the pointers inside the array point to the correct memory addresses
    for (int i = 0; i < sizeY; ++i)
    {
        array2d[i] = (temp + i * sizeX);
    }
    // Fill the array with ascending numbers
    for (int y = 0; y < sizeY; ++y)
    {
        for (int x = 0; x < sizeX; ++x)
        {
            array2d[y][x] = x + y * sizeX;
        }
    }
    // Code for testing
    // Print the addresses
    for (int y = 0; y < sizeY; ++y)
    {
        for (int x = 0; x < sizeX; ++x)
        {
            std::cout << std::hex << &(array2d[y][x]) << ' ';
        }
    }
    std::cout << "\n\n";
    // Print the array
    for (int y = 0; y < sizeY; ++y)
    {
        std::cout << std::hex << &(array2d[y][0]) << std::dec;
        std::cout << ": ";
        for (int x = 0; x < sizeX; ++x)
        {
            std::cout << array2d[y][x] << ' ';
        }
        std::cout << std::endl;
    }
    // Free memory
    delete[] array2d[0];
    delete[] array2d;
    array2d = nullptr;
    return 0;
}
当您调用大小为sizeX = 20且sizeY = 15的程序时,输出如下:
0x603010 0x603014 0x603018 0x60301c 0x603020 0x603024 0x603028 0x60302c 0x603030 0x603034 0x603038 0x60303c 0x603040 0x603044 0x603048 0x60304c 0x603050 0x603054 0x603058 0x60305c 0x603060 0x603064 0x603068 0x60306c 0x603070 0x603074 0x603078 0x60307c 0x603080 0x603084 0x603088 0x60308c 0x603090 0x603094 0x603098 0x60309c 0x6030a0 0x6030a4 0x6030a8 0x6030ac 0x6030b0 0x6030b4 0x6030b8 0x6030bc 0x6030c0 0x6030c4 0x6030c8 0x6030cc 0x6030d0 0x6030d4 0x6030d8 0x6030dc 0x6030e0 0x6030e4 0x6030e8 0x6030ec 0x6030f0 0x6030f4 0x6030f8 0x6030fc 0x603100 0x603104 0x603108 0x60310c 0x603110 0x603114 0x603118 0x60311c 0x603120 0x603124 0x603128 0x60312c 0x603130 0x603134 0x603138 0x60313c 0x603140 0x603144 0x603148 0x60314c 0x603150 0x603154 0x603158 0x60315c 0x603160 0x603164 0x603168 0x60316c 0x603170 0x603174 0x603178 0x60317c 0x603180 0x603184 0x603188 0x60318c 0x603190 0x603194 0x603198 0x60319c 0x6031a0 0x6031a4 0x6031a8 0x6031ac 0x6031b0 0x6031b4 0x6031b8 0x6031bc 0x6031c0 0x6031c4 0x6031c8 0x6031cc 0x6031d0 0x6031d4 0x6031d8 0x6031dc 0x6031e0 0x6031e4 0x6031e8 0x6031ec 0x6031f0 0x6031f4 0x6031f8 0x6031fc 0x603200 0x603204 0x603208 0x60320c 0x603210 0x603214 0x603218 0x60321c 0x603220 0x603224 0x603228 0x60322c 0x603230 0x603234 0x603238 0x60323c 0x603240 0x603244 0x603248 0x60324c 0x603250 0x603254 0x603258 0x60325c 0x603260 0x603264 0x603268 0x60326c 0x603270 0x603274 0x603278 0x60327c 0x603280 0x603284 0x603288 0x60328c 0x603290 0x603294 0x603298 0x60329c 0x6032a0 0x6032a4 0x6032a8 0x6032ac 0x6032b0 0x6032b4 0x6032b8 0x6032bc 0x6032c0 0x6032c4 0x6032c8 0x6032cc 0x6032d0 0x6032d4 0x6032d8 0x6032dc 0x6032e0 0x6032e4 0x6032e8 0x6032ec 0x6032f0 0x6032f4 0x6032f8 0x6032fc 0x603300 0x603304 0x603308 0x60330c 0x603310 0x603314 0x603318 0x60331c 0x603320 0x603324 0x603328 0x60332c 0x603330 0x603334 0x603338 0x60333c 0x603340 0x603344 0x603348 0x60334c 0x603350 0x603354 0x603358 0x60335c 0x603360 0x603364 0x603368 0x60336c 0x603370 0x603374 0x603378 0x60337c 0x603380 0x603384 0x603388 0x60338c 0x603390 0x603394 0x603398 0x60339c 0x6033a0 0x6033a4 0x6033a8 0x6033ac 0x6033b0 0x6033b4 0x6033b8 0x6033bc 0x6033c0 0x6033c4 0x6033c8 0x6033cc 0x6033d0 0x6033d4 0x6033d8 0x6033dc 0x6033e0 0x6033e4 0x6033e8 0x6033ec 0x6033f0 0x6033f4 0x6033f8 0x6033fc 0x603400 0x603404 0x603408 0x60340c 0x603410 0x603414 0x603418 0x60341c 0x603420 0x603424 0x603428 0x60342c 0x603430 0x603434 0x603438 0x60343c 0x603440 0x603444 0x603448 0x60344c 0x603450 0x603454 0x603458 0x60345c 0x603460 0x603464 0x603468 0x60346c 0x603470 0x603474 0x603478 0x60347c 0x603480 0x603484 0x603488 0x60348c 0x603490 0x603494 0x603498 0x60349c 0x6034a0 0x6034a4 0x6034a8 0x6034ac 0x6034b0 0x6034b4 0x6034b8 0x6034bc 
0x603010: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
0x603060: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 
0x6030b0: 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 
0x603100: 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 
0x603150: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
0x6031a0: 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 
0x6031f0: 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 
0x603240: 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 
0x603290: 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 
0x6032e0: 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 
0x603330: 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 
0x603380: 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 
0x6033d0: 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 
0x603420: 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 
0x603470: 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
如您所见,多维数组连续地位于内存中,并且没有两个内存地址重叠。 甚至释放数组的例程也比为每列(或行,具体取决于查看数组的方式)动态分配内存的标准方法更简单。 由于数组基本上由两个线性数组组成,因此仅这两个数组必须(可以被释放)。
具有相同概念的方法可以扩展到两个以上的维度。 在这里我不会做,但是当您有了它的想法时,这是一个简单的任务。
我希望这段代码能对您有所帮助。
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3159273