1. ホーム
  2. c++

3次元配列のベクトル実装

2022-02-22 12:05:02
<パス
#include 

#include 

int main()
{
    std::vector
 > >
 a(2);//create 2 vectors
 > Array of type

    for (int n = 0; n < 2; n++)
    {
        a[n].resize(4);
    }// To set the size of the two-dimensional array first

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            a[i][j].resize(2);
        }// Only after that can we set the size of the 3D array, otherwise there is a memory error
    }

    int m = 1;
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            for (int k = 0; k < 2; k++)
            {
                a[i][j][k] = m++;
            }
        }
    }

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            for (int k = 0; k < 2; k++)
            {
                std::cout << a[i][j][k] << std::endl;
            }
        }
    }
    std::cin.get();
    return 0;
}