Vector Iterator

J

JackC

Hi,

If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?

for example,

vector<MyClass*> myvector;
....
vector<MyClass*>::iterator it;

for(it = myvector.begin(); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember();
}

What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?

Thanks alot for any help,
 
E

Erik Wikström

Hi,

If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?

for example,

vector<MyClass*> myvector;
...
vector<MyClass*>::iterator it;

for(it = myvector.begin(); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember();
}

What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?

Use either

(*it)->myclassmember();

or

(**it).myclassmember();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top