Jump to content
  • Advertisement
Sign in to follow this  
utilae

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

This topic is 4936 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!