void CGame::RemoveVisibleEntity(entity_ptr pEntity)
{
for (std::vector::iterator p = VisibleEntityVector.begin(); p != VisibleEntityVector.end(); ++p)
{
if (pEntity == (*p))
{
VisibleEntityVector.erase((p));
return;
}
}
return;
}
If anyone could post example code showing how check if a pointer is in a vector, and if not, add it in, I would be really grateful.
std::vectors
How would I first check if a given pointer (boost shared_ptr) is in a vector, then remove it? the code to do it that I have is
if( find(v.begin(), v.end(), ptr ) == v.end() ) v.push_back( ptr );
But I think what you really want to use in this case is a std::set.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
But I think what you really want to use in this case is a std::set.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
That seems to work well enough in most cases, howver occasionally it fails. Could this be because the pointer == v.end()? is there any way to check?
How does it fail ? What happens ?
No, the fact that a pointer may be == to v.end() shouldn''t affect it (there are no objects stored at v.end()).
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
No, the fact that a pointer may be == to v.end() shouldn''t affect it (there are no objects stored at v.end()).
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Not with inserting, but with removing it fails..let me show some code:
I''m not really sure if this is proper way to do it, I had just adapted it from the previous answer. It works most of the time, but occasionaly one bullet or particle or whatever will just not go away. I followed it for a while and it just...doesn''t remove itself.
void CGame::RemoveEntity(entity_ptr pEntity){ if (std::find(EntityVector.begin(), EntityVector.end(), pEntity) == EntityVector.end()) { EntityVector.erase(EntityVector.end()); }}
I''m not really sure if this is proper way to do it, I had just adapted it from the previous answer. It works most of the time, but occasionaly one bullet or particle or whatever will just not go away. I followed it for a while and it just...doesn''t remove itself.
Ah. Since you're using a shared_ptr, it is easy :
Known as the erase-remove idiom.
Note that with a regular ptr, you'd have a memory leak.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 30, 2002 9:08:39 PM]
#include<vector>#include<algorithm>v.erase( std::remove( v.begin(), v.end(), ptr ), v.end() );
Known as the erase-remove idiom.
Note that with a regular ptr, you'd have a memory leak.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 30, 2002 9:08:39 PM]
Just tried it, and get the following error message:
cannot convert parameter 2 from ''const class boost::shared_ptr *(void) const'' to ''class boost::shared_ptr *''
Context does not allow for disambiguation of overloaded function
Again, I''ll show you the code:^
cannot convert parameter 2 from ''const class boost::shared_ptr *(void) const'' to ''class boost::shared_ptr *''
Context does not allow for disambiguation of overloaded function
Again, I''ll show you the code:^
void CGame::RemoveVisibleEntity(entity_ptr pEntity){ VisibleEntityVector.erase( std::remove( VisibleEntityVector.begin(), VisibleEntityVector.end(), pEntity ), VisibleEntityVector.end ); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement