Hello,

I have a vector of shared_ptr<> I'd like to erase all those that are reset. This is how I do it. I call operator! which should return true if it's reset. Is it a good approach. There are so many ways to implement things and I'm trying to develop a sense of what is a better way of doing things. Thanks.

template<typename T>
	void RemoveNulled(T& Vec)
	{
		// First remove from monitoring objects that no longer exist
		Vec.erase(
			std::remove_if(
				Vec.begin(),
				Vec.end(),
				boost::bind(&T::value_type::operator!, _1)),
			Vec.end());
	}

you would have to iterate through the vector and remove each element that is reset.
moving all the reset elements to the back of the sequence and then doing a single erase of elements at the end of the vector (as in your code) seems to be the most efficient way of doing it. you could also write

template<typename T>
void RemoveNulled(T& Vec)
{
  typename T::size_type new_sz = std::remove_if( Vec.begin(), Vec.end(), 
         boost::bind(&T::value_type::operator!, _1)) - Vec.begin() ;
  Vec.resize( new_sz ) ;
}

Sorry vijayan121, the way I understand your code is that I get vector full of NULLs back, because on line 5 all shared_ptr are destroyed during clear() call, if elements of the vector were their only references. Defaults for shared_ptr<> are NULLs and I'd like to remove those that are already NULL, but leave the rest inside.
Thanks.

> I'd like to remove those that are already NULL, but leave the rest inside.
i'm sorry, i had misread your code in my earlier post. and before seeing your reply, realized my mistake and edited my earlier post. my apologies for the confusion caused by this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.