CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2007
    Posts
    61

    [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!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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:
    Code:
    ++(iter->x);

  3. #3
    Join Date
    Jul 2007
    Posts
    61

    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
  •  





Click Here to Expand Forum to Full Width

Featured