Archived

This topic is now archived and is closed to further replies.

sdoherty55

Referencing STL Iterator Pointers

Recommended Posts

This is probably pretty easy, but I can''t seem to figure out how to reference the STL iterator pointer for my list? Thanks
  
typedef list<CShip> CShipList;

CShipList::iterator p;
for (p=m_oShipList.begin(); p!=m_oShipList.end(); p++){
   if (hr = p->Render() != D3D_OK){
	return hr;
   } 
   if (p !=  pPlayerShip){  // How do I reference the pointer?


  

Share this post


Link to post
Share on other sites

  
//Is this a typo?

typedef list<CShip> CShipList;

// Did you mean?

typedef list<CShip *> CShipList;


You can use the unary * (deference operator) to get back
the original CShip * from the iterator, assuming you are
storing CShip * in the first place.

*p will do.

Edit: added source tag

~~~~
Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on July 17, 2002 6:17:18 PM]

Share this post


Link to post
Share on other sites
No,

  
typedef list<CShip> CShipList;


was not a typo.

For some reason, I implemented CShip as a list of object, but implemented CExplosion as a list of pointers.


  
typedef list<CExplosion *> CExplosionList;


With out converting CShip to a list of pointers can I accesss there pointers?

Share this post


Link to post
Share on other sites

          

typedef list<CShip> CShipList;

CShipList::iterator p;

for (p = m_oShipList.begin(); p != m_oShipList.end(); ++p)
{
if (hr = p->Render() != D3D_OK)
return hr;

if (&(*p) != pPlayerShip)
{
// if the pointers are not equal

}
}



Is that what you want? (assuming the & operator for CShip isnt overloaded, otherwise you can use boost::addressof).

And you may wish to look at std::for_each for iterating through elements of an STL container....


  

#include <algorithm>
#include <functional>

for_each(m_oShipList.begin(), m_oShipList.end(), mem_fun_ref(CShip::Render));



I think that should work. I havent tried compiling anything mind.

[edit] - forgot #include <functional>

[edited by - sark on July 17, 2002 6:51:54 PM]

Share this post


Link to post
Share on other sites