将偏移量表示法转换为二维数组中的指针算术c ++

时间:2018-10-09 15:18:33

标签: c++ arrays pointers multidimensional-array pointer-arithmetic

因此,我尝试使用2d指针数组完成分配。当我意识到要求之一就是我应该使用指针算术,但我一直在使用过程时,我一直在使用偏移量表示法。所以我对你们的问题是,在不完全重写程序的情况下将偏移量表示转换为指针算术的最佳方法是什么?另外,在遍历2d数组时,我需要为outbounds函数调用哪些参数才能使其正常工作?任何建议将不胜感激,并预先感谢您。

    //move through string by parsing  to insert each char into array element position

void rules(char** boardArr,int  &rows, fstream inFile, string &line, int &cols)
{
    char* pos;
    char ncount;
    for(int i = 0; i < rows; i++) //rows
    {
        getline(inFile, line);

        for(int j = 0; j < cols; j++) //cols
        {
            *(*(boardArr + i )+ j) == pos;//parsing string into bArr

            //neighbor check nested for organism
            pos  = *(*(boardArr + i)+ j);//position of index within
            if(*(*(boardArr + i+1)+ j)=='*')//checking pos to the right of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j)=='*')//checking pos to the left of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i)+ j+1)=='*')//checking pos to the above of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i+1)+ j+1)=='*')//checking pos to the above and to the right of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j+1)=='*')//checking pos above and to the  left of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j-1)=='*')//checking pos below and to the left of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j)=='*')//checking pos below of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j+1)=='*')//checking pos below and to the right of pos index
            {
                //outofbounds()
                ncount++;
            }
            //row[i, row[i]-1])
            //cout<<*(*(boardArr + i)+ j);//assigning position to check for neighbors

        }



    }

//how to move through 2d array pointer arithmetic style

//boardArr[rows][cols] == *(*(boardArr + rows)+ cols)

//keep relationship between the numbers
//*(())
//If a cell contains an organism and has fewer than 2 neighbors, the organism dies of loneliness.
//A neighbor is an organism in one of the 8 spots (or fewer if on the edge) around a cell
//If a cell contains an organism and has more than 3 neighbors, it dies from overcrowding.
// If an empty location has exactly three neighbors, an organism is born in that location.
//returns nothing
}
bool  outofbounds( int &rows, int &cols, int i, int j)
{
    if((i >0 && i< rows)  && (j < cols && j > 0))
    {
        return true;
    }
    else
        return false;
}

1 个答案:

答案 0 :(得分:0)

没有理由为这种简单的操作使用指针算术。

只需使用arr[i][j]来读取/写入数据。

此外,您还应该在对存储器进行任何读/写操作之前检查边界。这很危险,可能会导致程序崩溃。

这是我如何实现这些内容的版本。

#include <iostream>


/* it is good practice to move functions with special context to classes */
class SafeCharMatrix
{

private:

    /* your board */
    /* `char const* const*` provides that nobody can change data */
    char const* const* _ptr;
    int _rows;
    int _cols;

public:

    SafeCharMatrix(char const* const* ptr, int rows, int cols) :
        _ptr(ptr), _rows(rows), _cols(cols)
    {}

    /* valid check bounds algorithm */
    bool CheckBounds(int x, int y) const
    {
        if (x < 0 || x >= _cols)
            return false;

        if (y < 0 || y >= _rows)
            return false;

        return true;
    }

    bool CheckCharSafe(int x, int y, char c) const
    {
        /* check bounds before read/write acces to memory */
        if (!CheckBounds(x, y))
            return false;

        return _ptr[x][y] == c;
    }

    int CountNeighborsSafe(int x, int y, char c) const
    {
        int count = 0;

        count += CheckCharSafe(x - 1, y - 1, c) ? 1 : 0;
        count += CheckCharSafe(x - 1, y    , c) ? 1 : 0;
        count += CheckCharSafe(x - 1, y + 1, c) ? 1 : 0;
        count += CheckCharSafe(x    , y - 1, c) ? 1 : 0;
        /* ignore center (x, y) */
        count += CheckCharSafe(x    , y + 1, c) ? 1 : 0;
        count += CheckCharSafe(x + 1, y - 1, c) ? 1 : 0;
        count += CheckCharSafe(x + 1, y    , c) ? 1 : 0;
        count += CheckCharSafe(x + 1, y + 1, c) ? 1 : 0;

        return count;
    }

};


/* fill you board before this */
void rules(char const* const* boardArr, int rows, int cols)
{
    SafeCharMatrix matrix(boardArr, rows, cols);

    for (int i = 0; i < rows; ++i) /* y axis */
    {
        for (int j = 0; j < cols; ++j) /* x axis */
        {
            int countOfNeighbors = matrix.CountNeighborsSafe(j, i, '*');

            /* do whatever you want */

            std::cout
                << "x: " << j << ", "
                << "y: " << i << ", "
                << "count: " << countOfNeighbors << "\n";
        }
    }
}


/* just example of how it can works */
int main()
{
    char r1[3] = {  0 ,  0 , '*'};
    char r2[3] = {  0 ,  0 ,  0 };
    char r3[3] = { '*',  0 ,  0 };

    char* m[3];
    m[0] = r1;
    m[1] = r2;
    m[2] = r3;

    rules(m, 3, 3);
}

编辑:

请勿通过引用传递简单的参数,例如int数字int &row。它们很小,编译器可以将它们打包在一个处理器寄存器中。