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

Started by
2 comments, last by utilae 19 years, 2 months ago
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]

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

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.
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    }}
FTA, my 2D futuristic action MMORPG
Ok, Neex, thanks. I figured I was right, though I forgot about the iterators with different types, etc.

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

This topic is closed to new replies.

Advertisement