Search code examples
c++arraysmaze

Get array size for use in for loop


I'm trying to get the size of my tiles array so it can be used in the for loop below. However, when compiling, it is showing that .size() is experimental in C++ 2011. What would be the best way to get the size in relation to this loop which checks each tile for the 'E' character? I'm using array instead of a vector so please assist me with arrays.

void Floor::placePlayer()
{
    bool foundEntry = false;
    int entryX;
    int entryY;

    for (int x = 0; x < tiles.size() && !foundEntry; x++)
    {
        for (int y = 0; y < tiles[x].size() && !foundEntry; y++)
        {
            if (tiles[x][y] == 'E')
            {
                player.set_position (x,y);
                tiles[x][y] = 'P';
            }
        }
    }
}

Also this is my array from the header:

const static int ROWS = 20;
const static int COLS = 30;
char tiles[ROWS][COLS];

Thanks for the help!


Solution

  • Why don't you use your const static int used to create the array ?

    void Floor::placePlayer()
    {
        bool foundEntry = false;
        int entryX;
        int entryY;
    
        for (int x = 0; x < ROWS && !foundEntry; x++)
        {
            for (int y = 0; y < COLS && !foundEntry; y++)
            {
                if (tiles[x][y] == 'E')
                {
                    player.set_position (x,y);
                    tiles[x][y] = 'P';
                }
            }
        }
    }