You can also use a function or combination of function and bind and achieve the same result.
I think I read somewhere that using a function object is more efficient since it can be better inlined. But that's not the main reason I prefer function objects. The main reason is that you maybe have a higher initial complexity (extra class) but the solution scales well with more demanding requirements (take for example the case when you want to reduce identical subsequent entries to only one occurence or other cases where you need to store some information from call to call).
And if you look at the next standard of C++, there will be lambda expressions which will probably supercede both functions and function objects in this case.
If you want to reduce the need to write too many classes, use functor templates that you can reuse more easily!
1 2 3 4 5 6 7
|
template<typename T>
class ComparesEqualTo
{
T _ref;
ComparesEqualTo(const T& ref) : _ref(ref) {};
bool operator()(const T& val){return val == _ref;};
}
|
But it your case (with the IP addresses) I would stick with a functor specially designed for this purpose and the remove/erase idiom.
If you're interested you can take a look at boost lambda (
http://www.boost.org/doc/libs/1_45_0/doc/html/lambda.html) which a adds limited support for lambda functions or if you are using Visual Studio 2010 you can use "native" lambda functions (
http://msdn.microsoft.com/en-us/library/dd293608.aspx).
With the VS2010 lambda function it is possible to do it without an extra function or function object.