std::list::remove_if predicate

This is an example of a predicate to be used with std::list::remove_if.

template <class T> class CHECK_RETIRE : public std::unary_function<T,bool>
{
	private:
		time_t now;
	public:
		CHECK_RETIRE(time_t n) : now (n) { }

		bool operator() ( T val )
		{
			// T is a shared_ptr. If the member ->retire is less than now, return true to remove me.
			return val->retire < now;
		}
};

// Usage
struct CHECK
{
	time_t retire;
}

typedef boost::shared_ptr<CHECK>	 CHECK_ptr;

std::list<CHECK> checklist:

/* add stuff */

time_t now = 1000;
checklist.remove_if( CHECK_RETIRE<CHECK>(now) );

// Items with ->retire less than 1000 is now removed from the list.
VN:F [1.9.13_1145]
Rating: 5.0/5 (1 vote cast)
std::list::remove_if predicate, 5.0 out of 5 based on 1 rating
Share

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)