vectors and vector iterators

Oct 12, 2012 at 3:56pm
Vectors are new to me and I am having a hard time finding documentation on how to use them. I am also wondering how to use their iterators.

So my goal is to create a vector (essentially a dynamic array) of references/pointers of type Account. I then need to have insert/remove and find functions (presumably using an iterator) for the vector.

Any ideas?
Oct 12, 2012 at 4:07pm
Use std::vector<Account *> :)
Oct 12, 2012 at 4:10pm
http://www.cplusplus.com/reference/stl/vector/

There's documentation for it. The methods you're looking for is push_back(), remove(), and to find something you'll need to write your own function for that.

http://www.cplusplus.com/reference/std/iterator/

Here's documentation on iterator. Gotta be careful using iterator with vector, whenever it resizes any pointers you have to that vector (ie iterators) could become invalidated and you'll need to reset them.
Oct 12, 2012 at 4:11pm
Yeah, I was playing around with std::vector<Account*> and that seems to hold the pointers to Account just fine but then how do I implement random access. I am getting stuck with this-> or * or & and how I can implement things like find.
Would it look like

1
2
3
4
5
for(int acc = 0; acc != accounts.end(); acc++)
{
  if(acounts[acc].getName == "Bob")
    ....
}


I am not sure if I need an iterator or if I have to use accounts* or accounts& or accounts->.

Thanks.
Oct 12, 2012 at 4:17pm
I don't see remove(). Did you mean erase()?
Oct 12, 2012 at 4:27pm
Ah yea I meant erase(). Not sure why I said remove().
Topic archived. No new replies allowed.