• Advertisement
Sign in to follow this  

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

This topic is 4755 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
Advertisement
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

for(std::list<MAINSTRUCT*>::iterator it = m_lstMainList.begin();
it != m_lstMainList.end(); ++it)
{

for(std::list<RECT*>::iterator it2 = (*it)->m_lstRectList.begin();
it2 != (*it)->m_lstRectList.end(); ++it2)
{

// do stuff here
}
}

Share this post


Link to post
Share on other sites
Ok, Neex, thanks. I figured I was right, though I forgot about the iterators with different types, etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement