简体   繁体   English

如何通过C ++中的指针使用3D数组

[英]How to work with 3d arrays through pointers in C++

I'm facing some difficulties with pointers for 3D arrays in C++. 我在C ++中使用3D数组的指针时遇到一些困难。 I've a array of Q[3][N][N] which I want to pass to a function to print the values at [0][i][j] location (and also for [1][i][j] and [2][i][j]). 我有一个Q[3][N][N]数组,我想传递给该函数以在[0][i][j]位置(以及[1][i][j][2][i][j]). How can I achieve this? 我该如何实现? Will it be more convenient to use Q[i][j][0] etc? 使用Q[i][j][0]等会更方便吗?

for 2D, the following piece of code works just fine when I give &Q[0][0] to the *var: 对于2D,当我将&Q [0] [0]赋予* var时,下面的代码可以正常工作:

template <typename T>
void print2d(T *var, int I, int J){
cout << endl;
for (int j = 0; j < J; j++){
    for (int i = 0; i < I; i++){
        cout << setprecision(3) << setw(12) << *(var + (N*i + j));
    }
    cout << endl;
}
cout << endl;
}

I'm using the same approach to write a similar function for 3D which does not write out the correct values: Can anybody let me know the correct way to point to the correct address of Q[i][j][1] . 我正在使用相同的方法为3D编写类似的函数,但该函数不会写出正确的值:谁能让我知道指向Q[i][j][1]正确地址的正确方法。 In input argument, I'm giving the address of Q[0][0][0]. 在输入参数中,我给出了Q [0] [0] [0]的地址。 Should I use different addresses (such as Q[i][j][1]) if I want to write out for that particular value of k? 如果要写出k的特定值,是否应该使用其他地址(例如Q [i] [j] [1])?

template <typename T>
void print3d(T *var, int I, int J, int K){
    cout << endl;
        for (int j = 0; j < J; j++){
            for (int i = 0; i < I; i++){
                cout << setprecision(3) << setw(12) << *(var + I*J*i + I*j + K);
            }
            cout << endl;
        }
    cout << endl;
}

Sample example, N must be const. 示例示例中,N必须为const。 But consider using vector. 但是考虑使用向量。

#include <stdio.h>
#include <iostream>
using namespace std;
const int N = 5;

void func2(int* tab, int A, int B, int C)
{
    printf("%d\n", tab[N*N*A + N*B + C] );
}

void func(int tab[3][N][N])
{
    for (int i = 0; i < 3; ++i)
    for (int j = 0; j < N; ++j, printf("\n"))
    for (int k = 0; k < N; ++k)
    {
        printf("%d ", tab[i][j][k]);
    }
}
int main()
{
    int tab[3][N][N];
    int p = 0;
    for (int i = 0; i < 3; ++i)
    for (int j = 0; j < N; ++j)
    for (int k = 0; k < N; ++k)
    {
        tab[i][j][k] = p++;
    }
    func(tab);
    printf("\n");
    func2((int*)tab, 1, 1, 3);
}

I agree with Adam's answer but cant comment, need more points there. 我同意亚当的回答,但不能发表评论,需要在这里多加说明。

1) Arrays are not pointers. 1)数组不是指针。 Multidimensional arrays cannot be passed via a pointer, needs to have all but one dimension constant. 多维数组不能通过指针传递,需要具有除一维之外的所有常量。 Here is an answer to help understand. 这是帮助您理解的答案。

Cannot cast array to pointer 无法将数组转换为指针

How do I pass a reference to a two-dimensional array to a function? 如何将对二维数组的引用传递给函数?

http://www.cplusplus.com/forum/articles/17108/ http://www.cplusplus.com/forum/articles/17108/

You might want to try vectors from STL instead. 您可能想尝试使用STL中的向量。

2) Templates are compiled only when there is an instance of it used, that is why its declaration and usage is generally put in one file. 2)仅在使用模板实例时才编译模板,这就是为什么模板的声明和用法通常放在一个文件中的原因。 Template Compilation 模板编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM