yuval
yuval

Reputation: 3088

having problems using iterators with lists in c++

I am writing an algorithm that checks for duplicate moves in checker in c++ but I am having problems with using the iterator.

Here is my code:

static std::list<char[8][8]> duplicates;   

    char checkForDuplicates[8][8];
    for(char i = 0;i < 8;i++)
        for(char j = 0;j < 8;j++)
            checkForDuplicates[i][j] = board[i][j];
    bool isDuplicate = false;
    for(std::list<char[8][8]>::iterator it = duplicates.begin(); it != duplicates.end(); it++)
    {
        for(char i = 0;i < 8;i++)
        {
            for(char j = 0;j < 8;j++)
            {
                if(checkForDuplicates[i][j] != it->[i][j])
                    //do something
            }
        }
    }   

I especially need help with this line:

if(checkForDuplicates[i][j] != it->[i][j])

I am trying to compare an array element in the list to another array element but I have no idea how to access the array elements inside the list using the list iterator. Can anybody please assist me with examples and/or advice?

Upvotes: 0

Views: 289

Answers (2)

devtk
devtk

Reputation: 2179

It may may things easier to understand if you create a reference to the object from the iterator. After that, you can access it how you normally would.

static std::list<char[8][8]> duplicates;   

    char checkForDuplicates[8][8];
    for(char i = 0;i < 8;i++)
        for(char j = 0;j < 8;j++)
            checkForDuplicates[i][j] = board[i][j];
    bool isDuplicate = false;
    for(std::list<char[8][8]>::iterator it = duplicates.begin(); it != duplicates.end(); it++)
    {
        char (&thisBoard)[8][8] = *it;  # <--
        for(char i = 0;i < 8;i++)
        {
            for(char j = 0;j < 8;j++)
            {
                if(checkForDuplicates[i][j] != thisBoard[i][j])
                    //do something
            }
        }
    }

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308432

Instead of it->[i][j] use (*it)[i][j]. The -> notation is only used when you're accessing a member, but your list contains arrays, not classes or structs.

Upvotes: 3

Related Questions