【问题标题】:How to store symmetries of a Polyomino using pointers-to-pointers?如何使用指针对指针存储 Polyomino 的对称性?
【发布时间】:2015-05-13 21:53:23
【问题描述】:

好的,我正在编写离散平铺问题的代码。我正在存储名为polyomino 的对象,如下所示:

 pointer = new int*[h];
 p_0 = new int*[h];
 p_1 = new int*[h];
 p_2 = new int*[w];
 p_3 = new int*[w];
 p_4 = new int*[w];
 p_5 = new int*[h];
 p_6 = new int*[w];

 for (i=0 ; i < h ; i++)
     pointer[i] = new int[w];
     p_0[i] = new int[w];
     p_1[i] = new int[w];
     p_5[i]=new int[w];

     } 
     for(i=0 ; i < w ; i++){
     p_2[i]=new int[h];
     p_3[i]=new int[h];
     p_4[i]=new int[h];
     p_6[i]=new int[h];
     }

  for (i=0; i<h ; i++){
     for(j=0; j<w ; j++){
        cout << "What is the " << i+1;
          cout << ", " << j+1;
           cout << endl;
             cin >> k;
              if(k != 0)
                pointer[i][j]=1;      
                 else
                  pointer[i][j]=0;

      } 


} 

然后我生成所有 8 个可能的方向(正方形的group of symmetries)并将它们存储到其他 7 个双指针中。我想知道是否有一种方法可以制作大小为 8 的数组来保存每个双指针的地址。

如果我能做到这一点,那么在检查要平铺的木板是否有空位然后放置它们时,我的生活会轻松很多。 我想要类似的东西:

         orientation[8];
         orientation[0]=pointer;
         orientation[1]=p_0;
         orientation[2]=p_1;
         orientation[3]=p_2;
         orientation[4]=p_3;

等等。问题是有两种不同的尺寸;一个是hxw,另一个是wxh。我的第一个想法是:

     int** orientation;
     orientation = new int*[8]

     for(i=0;i<8<;i++)
       orientation[i]=new int*[h*w];

提前致谢。

【问题讨论】:

  • 我本质上是想让我的所有方向在内存中彼此直接相邻。
  • 我不明白你现在在做什么,什么能让你的生活更轻松。也许发布更多代码或改写问题?
  • 我的意思是指向指针的指针。我想要一种方便的方式来访问我的八个二维矩阵(我已经生成并存储在内存中。我没有包含这部分代码。如果需要的话我会这样做)。我遇到的问题是我的 4 个矩阵是 hxw,4 个是 wxh。我想要类似 Orientation[8] 的东西
  • 方向 [0] = 指针;方向[1] = p_0;等等……

标签: c++ arrays pointers tiling


【解决方案1】:

数组的元素存储在内存中的连续位置,因此您可以像这样获取地址并对其进行一些计算:

int arr[] = { 0, 1, 2, 3, 4, 5 };
int* a = &arr[0]; // Address of the beginning of the array (i.e index 0)
int* b = a + 3;   // Address of index 3, behind the scene it does a + (3 * sizeof(int))

cout << "index 0 : " << *a << endl;
cout << "index 3 : " << *b << endl << endl;

int d = 0, e = 1, f = 2;
int* arr2[] = { &d, &e, &f }; // Array of pointers
int** address = &arr2[1];     // Pointer to a pointer
int*** address_of_handle = &address; // Address of that pointer
cout << "index 1 : " << ***address_of_handle;

但除了熟悉指针之外,我想知道您为什么要这样做?

【讨论】:

  • 出于简单的“簿记”原因,我想这样做。我觉得将我的所有矩阵组合在一起可能很方便。最后可能没用。不过,谢谢你的回复!这帮助了一堆
【解决方案2】:

我很抱歉,但他错了你可以从 0 或 1 开始你的数组,只要你在正确的位置停止你的数组如果你试图到达数组之外的内存你会遇到溢出问题亚历克斯你应该读一点更多的 C/C++

【讨论】:

    【解决方案3】:

    你可以像这样声明一个大小为 8 的双精度指针数组

    双* Arrptr[8];

    然后要存储地址,你只需要使用一个循环,它可以是一个 for 循环,将每个值存储在一个数组中,但是你必须在数组上一个接一个地保存这个解决方案会更好

    double *arrptr[8];
    for (int i=1;i<=8;i++)
    {
        /*generate values getting them from the user or 
        you can generate them with your method*/
    
        cout << "Enter orientations/n";
        cin >> arrptr[i];
    
    
    }
    

    【讨论】:

    • 数组在 C 和 C++ 中是零索引的。这会导致溢出并可能导致段错误。
    • 感谢您的输入,即使您的索引在 for 循环中已关闭。不过,我认为这种方法不适用于我的情况。
    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2014-12-19
    • 2015-07-13
    相关资源
    最近更新 更多