std::vectors

Started by
7 comments, last by Laroche 21 years, 10 months ago
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

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.
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
Advertisement
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Not with inserting, but with removing it fails..let me show some code:

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.
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
Ah. Since you're using a shared_ptr, it is easy :


    #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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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:^

void CGame::RemoveVisibleEntity(entity_ptr pEntity){	VisibleEntityVector.erase( std::remove( VisibleEntityVector.begin(), 		VisibleEntityVector.end(), pEntity ), VisibleEntityVector.end );	} 

Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
typo in my code. Missed the pair of () for the last v.end()

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:59 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ah thanks millions! Everything works properly now!
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content

This topic is closed to new replies.

Advertisement