> What if I didn't want to pass in the element of a container...what if I wanted to pass in the string length of each element?!
I don't think this should be a problem. For example, if you were using find_if on a container of strings, and you wanted to find an element with length equal to 7, you can fairly easily write your own functor, e.g.:
Code:
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
template <class T>
struct length_is_equal: public std::binary_function<T, typename T::size_type, bool>
{
bool operator()(T obj, typename T::size_type len) const
{
return obj.length()==len;
}
};
int main()
{
std::vector<std::string> strings;
strings.push_back("Hello");
strings.push_back("World");
strings.push_back("abcefg");
std::vector<std::string>::iterator it = std::find_if(strings.begin(),strings.end(), std::bind2nd(length_is_equal<std::string>(),5));
if (it!=strings.end())
std::cout<<*it;
}
Granted, the syntax can be a bit verbose/ugly, but it's not too difficult when you get used to it. (And if you use boost, it might be able to simplify things somewhat)