More trouble with iterators, multidim deques

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

More trouble with iterators, multidim deques

Postposted on Tue Oct 18, 2011 3:07 pm

This following code works perfectly:
Code: Select all
   
(in the header file)
   deque<deque<deque<int> > > openlist;
   deque<deque<deque<int> > >::iterator openlist_Iterator;
(in the cpp file)
for(int i=-1;i<=1;i++) //For the 8 surrounding pixels around some pixel (initially startPixel), find white pixels and add them to open list
   {
      for(int j=-1;j<=1;j++)
      {
         currentcolor=image.pixel(currentPixel[0]+i,currentPixel[1]+j);
         if(qGreen(currentcolor)==g && qRed(currentcolor)==r && qBlue(currentcolor)==b)
         {
            openlist[0][0].push_back(currentPixel[0]+i); //openlist[0] is the position of the pixel on the open list
            openlist[0][1].push_back(currentPixel[1]+j);
            openlist[1][0].push_back(currentPixel[0]); //openlist[1] is its parent pixel
            openlist[1][1].push_back(currentPixel[1]);
            if((i==0 || j==0) && !(i==0 && j==0))//openlist[2][0] is the G cost
               {openlist[2][0].push_back(10);}
            else
               {openlist[2][0].push_back(14);}
            openlist[2][1].push_back(manhattan());//openlist[2][1] is the H cost
         }
      }
   }

The only reason I'm using iterators is to try out something new. According to http://www.cprogramming.com/tutorial/stl/iterators.html the following works:
Code: Select all
for(myIntVectorIterator = myIntVector.begin();
        myIntVectorIterator != myIntVector.end();
        myIntVectorIterator++)
{
    cout<<*myIntVectorIterator<<" ";
    //Should output 1 4 8
}

But actually this doesn't? WTF? VS2003 gives me an error C2100 about not being able to dereference something that isn't a pointer. I don't know how else to do this.
Code: Select all
   for(openlist_Iterator=openlist.begin();openlist_Iterator!=openlist.end();openlist_Iterator++) //find the lowest F cost square
   {
      (*openlist_Iterator[2][0])+(*openlist_Iterator[2][1]);
   }


Are the new features in C++ supposed to be THIS hard to learn? BTW I have no clue how to traditionally (C array style) access the xth element of a 3D deque, where an element of the 3D array in this program's case has 6 values.
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2252
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

Re: More trouble with iterators, multidim deques

Postposted on Tue Oct 18, 2011 4:57 pm

Code: Select all
   for(openlist_Iterator=openlist.begin();openlist_Iterator!=openlist.end();openlist_Iterator++) //find the lowest F cost square
   {
      (*openlist_Iterator)[2][0]+(*openlist_Iterator)[2][1];
   }


not sure exactly what you are trying to do, but [] is higher precedence than *, and the iterator doesnt have a [] operator, hence the compile error.


Is this a 3D array - do you have openlist[i][j][k], or are the first two indices just 0-2 and 0-1? I would suggest something like

Code: Select all
struct foo
{
int posi, posj, parenti, parentj, Gcost, Hcost
//or int pos[2], parent[2], Gcost, Hcost
};

deque<foo> openlist


and then
Code: Select all
for(openlist_Iterator=openlist.begin();openlist_Iterator!=openlist.end();openlist_Iterator++) //find the lowest F cost square
   {
      openlist_Iterator->Gcost + openlist_Iterator->Hcost
   }


or perhaps:
Code: Select all
deque<int> posi, posj, parenti, parentj, Gcost, Hcost


because I don't see the 3D-ness of your array - you have a list (or deque) of pixels, and a fixed number of properties associated with each pixel, so either organise these properties together and have a list of structs, or have a struct of lists, which might be more efficient if you are iterating over the properties independently of each other, or might not be.
mboza
Gerbil Team Leader
 
Posts: 203
Joined: Fri Sep 15, 2006 6:52 am

Re: More trouble with iterators, multidim deques

Postposted on Sun Oct 30, 2011 12:12 pm

Thanks, you're right... a struct would be a better fit. Anyway the rules have changed, and I've scrapped the code anyway. Thanks, I learned about structs and how other people use them.
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2252
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest