Search code examples
c++arraysfor-loopiteratordeque

Printing the First Array in a Deque of Structs


I have a Deque that contains this kind of stucts.

struct New_Array {                    
    array<array<int,4>,4> mytable;       
    int h;
};

In this stuct 2 different arrays may have same value of h.

deque<New_Array> Mydeque;

I also know how many different h are in the deque(the value of steps). And how many stucts are in the deque(Mydeque.size()).

I need to print one array for each h. Starting from h=0 till h=steps (steps is a known int value). Each array that is going to be printed must be the closer to the end of the deque.

I tried something like this:

void foo(deque<New_Array> Mydeque, int steps)
 for(int i=0; i<steps; i++)
     {
        deque<New_Array>::iterator it;
        it = find(Mydeque.begin(),Mydeque.end(),i);
        PrintBoard(*it); // This if a function where you enter the New_Array struct 
                         // and it prints the array         
     }
}

The above gives me : error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const bool' (or there is no acceptable conversion)

Or something like this:

void foo(deque<New_Array> Mydeque, int steps)
for(int i=0; i<steps; i++)
     {
        deque<New_Array>::iterator it;
        for(unsigned int j=0;j<Mydeque.size();j++)
            {
            it = find_if(Mydeque.begin(),Mydeque.end(),Mydeque[j].h==i);
            PrintBoard(*it);
            break;
            }           

     }

The above gives me: error C2064: term does not evaluate to a function taking 1 arguments

EDIT: The deque is not sorted. For each h an array should be printed. This array should be the one that is at this moment closer to the end of the deque.


Solution

  • The solution is :

    void Find_Solution_Path(deque<New_Array> Mydeque, int steps)
    {
    for(int i=0; i<steps+1; i++)
        {
            for(int j=Mydeque.size()-1;j>-1;j--)
            {
                if (Mydeque[j].h==i)
                {
                    PrintBoard(Mydeque[j]);
                    cout<<endl;
                    break;
                }    
            }    
        }
    }