|
-
August 7th, 2008, 01:56 PM
#1
[RESOLVED] Accessing a vector element
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:
Code:
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 ->)?
Thanks for the help!
-
August 7th, 2008, 02:01 PM
#2
Re: Accessing a vector element
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:
Code:
iter != dynamic.end()
Second, you'd then do:
-
August 7th, 2008, 02:07 PM
#3
Re: Accessing a vector element
Thanks a lot. It worked like a charm =)
Thanks for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|