繁体   English   中英

使用指针迭代多维数组

[英]Using pointers to iterate through multidimensional array

如果你可以使用指针迭代这样的数组:

for (int *iter = arr; iter != std::end(arr); ++iter) {
    // code
}

如何使用指针迭代多维数组(不使用auto )?

编辑:我假设这是一个int[][] ,如{{3, 6, 8}, {2, 9, 3}, {4, 8, 2}}

如果你将数组声明为arr [] [],那么你可以因为它们按顺序存储在内存中。 你可以做:

for(int * iter = &arr[0][0]; iter != &arr[0][0] + col * row; iter++)
//...

怎么样这样: -

const int* d = data;
for ( int i = 0; i < width; ++i )
    for ( int j = 0; j < height; ++j )
        for ( int k = 0; k < depth; ++k )
            sum += *d++;

查看本教程

可能是这样的

   for (int **iter = arr; iter != std::end(arr); ++iter) {
        for (int *iter2 = *iter; iter2 != std::end(*arr); ++iter2) {
        // code
        }
    }

例如:

constexpr size_t rowCnt = 3, colCnt = 4;

int ia[rowCnt][colCnt] = { // three elements; each element is an array of size 4
     {0, 1, 2, 3}, // initializers for the row indexed by 0
     {4, 5, 6, 7}, // initializers for the row indexed by 1
     {8, 9, 10, 11} // initializers for the row indexed by 2
    };

不使用类型别名作为循环控制变量的类型:

// p points to the first array in ia
for (int (*p)[colCnt] = ia; p != ia + rowCnt; ++p) {
    // q points to the first element of an array of four ints; that is, q points to an int
    for (int *q = *p; q != *p + colCnt; ++q)
        cout << *q << " ";
    cout << endl;
}

或者更容易使用auto

for (auto p = ia; p != ia + rowCnt; ++p) {
    // q points to the first element of an array of four ints; that is, q points to an int
    for (auto q = *p; q != *p + colCnt; ++q)
        cout << *q << " ";
    cout << endl;
}

使用sizeof技巧改进texasbruce的答案(无法评论抱歉),假设数组是静态分配的(维度在编译时是已知的) for(int * iter = &arr[0][0]; iter != &arr[0][0] + sizeof(arr)/sizeof(int); iter++)iter != (int*)((void*)&arr[0][0] + sizeof(arr))如果你是(void *)粉丝并且讨厌任何编译时的部门

所以你不必费心数组维度:)

假设您只是在谈论静态声明的多维数组:

const int ROWS = 10;
const int COLS = 20;
const int DEPTH = 30;
int array[ROWS][COLS][DEPTH]; // statically allocated array
int i = 0;
for (int row = 0; row < ROWS; ++row)
{
    for (int col = 0; col < COLS; ++col)
    {
        for (int depth = 0; depth < DEPTH; ++depth)
        {
            *(*(*(array + row) + col) + depth) = i++; // do whatever with it
            // is equivalent to array[row][col][depth] = i++;
        }
    }
} 

如果您需要更多级别,您只需继续添加指针间接级别。

或者:

const int ROWS = 5;
const int COLS = 6;
const int DEPTH = 3;
int array[ROWS][COLS][DEPTH]; // statically allocated array
int* p = &array[0][0][0];
int c = 0;
for (int i = 0; i < ROWS * COLS * DEPTH; ++i, p++)
{
    *p = c++;
}

由于静态声明的数组在内存中是连续的,第一个元素( array[0][0][0] )从基址( &array )开始,这是有效的。

动态声明一个多维数组将成为指向(etc)指向object_type数组指针的指针数组的指针。 您可以使用std::vectorstd::array来简化它(如果您知道编译时的大小)。

并非这些指针使用指针进行迭代(至少不是直接),但是

矢量/阵列

std::vector<std::vector<std::vector<int> > > array;
// or
//std::array<std::array<std::array<int, DEPTH>, COLS>, ROWS> array; // with some slight modifications
// fill in the vectors/arrays however you want

std::for_each(array.begin(), array.end(), [](const std::vector<std::vector<int> >& v)
{
    std::for_each(v.begin(), v.end(), [](const std::vector<int>& a)
    {
        std::for_each(a.begin(), a.end(), [](int i)
        {
            std::cout << i << endl;
        });
    });
});

暂无
暂无

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

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