Click to See Complete Forum and Search --> : [RESOLVED] Accessing a vector element


nerdykid
August 7th, 2008, 01:56 PM
I have a struct called OBJECT and a vector called dynamic. the vector "dynamic" is a vector of the type OBJECT. My problem is that when I try to iterate through the vector, I cannot access the elements of OBJECT using the vector. For example:

vector<OBJECT> dynamic;

void LoadVector(OBJECT go[])
{
for(int i = 0; i <= 24; i++)
{
if(go[i].movable)
dynamic.push_back(go[i]);
}
}

void PullDown(OBJECT go[])
{
for(vector<OBJECT>::iterator iter = dynamic.begin(); iter < dynamic.end(); iter++)
{
++dynamic[iter].x; //This is where the problem is. Apparently it cannot access x
}
}

Is there any way to access an element from the vector (I have also tried ->)?

Lindley
August 7th, 2008, 02:01 PM
iter isn't an index. It's not a pointer either, but it's closer to the truth if you think of it that way.

First, the correct test is:
iter != dynamic.end()
Second, you'd then do:
++(iter->x);

nerdykid
August 7th, 2008, 02:07 PM
Thanks a lot. It worked like a charm =)