Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

More trouble with iterators, multidim deques

Tue Oct 18, 2011 3:07 pm

This following code works perfectly:
   
(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:
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.
   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: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
mboza
Gerbil Team Leader
Posts: 203
Joined: Fri Sep 15, 2006 6:52 am

Re: More trouble with iterators, multidim deques

Tue Oct 18, 2011 4:57 pm

   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

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

deque<foo> openlist


and then
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:
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.
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: More trouble with iterators, multidim deques

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: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230

Who is online

Users browsing this forum: No registered users and 20 guests
GZIP: On