Iterating a C++ std::list

Set up a list to store pointers to a “Points” class…

list<Point*> m_pointList;

Add to the list…

Point* p = new Point(x, y);
m_pointList.push_back(p);

Then iterate the list using this…

std::list<Point*>::iterator it = m_pointList.begin();
while (it != m_pointList.end())
{
    Point* p = *it;

    // do something with the Point here!

    it++;
}

…or, if you’d prefer a for loop…

for(std::list<Point*>::iterator it = m_pointList.begin(); it != m_pointList.end(); ++it)
{
    Point* p = *it;
    // do something with the Point here!
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: