Sign in to follow this  

How would I access a list within a list using iterators?*solved*

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Below I have a struct and a list in a class. In the struct is another list, of RECTs.
struct MAINSTRUCT
{    
    int m_nWidth;         
    int m_nHeight; 
    list <RECT*> m_lstRectList;
};

static list <MAINSTRUCT*> m_lstMainList;


Using an iterator, the main list is accessed like this:
list<MAINSTRUCT*>::iterator it=m_lstMainList.begin();


So how would I access a list within a list? I'll try and see if I get it right.
list<MAINSTRUCT*>::iterator it=m_lstMainList.begin();
//loop through and find the element of the list I want (would be done here)
it=(*it)->m_lstRectList.begin();


[Edited by - utilae on February 13, 2005 2:52:28 PM]

Share this post


Link to post
Share on other sites
You did this:
list<MAINSTRUCT*>::iterator it=m_lstMainList.begin();
//loop through and find the element of the list I want (would be done here)
it=(*it)->m_lstRectList.begin();

That looks almost right. Here's what I'd put:
list<MAINSTRUCT*>::iterator it = m_lstMainList.begin();
list<RECT*> it2 = (*it)->m_lstRectList.begin();

The difference is you're using the same iterator (it) to iterate through the outer and inner lists. Which won't work for a few reasons, one of which is that they're different types.

Share this post


Link to post
Share on other sites

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this