It seems to be stupid question but I have never seen something like this!
int operator() (const std::string& s1, const std::string& s2) const
to me operator is a const member function that return an intger and no variable argument!
but need two const variables s1 and s2?!?! I can’t get the point of this!
class str_dist_ :
public std::binary_function<std::string, std::string, int> {
public:
int op() (const std::string& s1, const std::string& s2) const
{
if (s1 == s2)
return 0;
else if (s1.empty())
return std::max(s2.size()/2, 1u);
else if (s2.empty())
return std::max(s1.size()/2, 1u);
else
return min_edit_distance(s1, s2, zero_one_cost<char>(' '));
}
std::string empty() const { return std::string(); }
};
The
operator()part says that objects of typestr_dist_can be “called”, as though it were a function. The(const std::string& s1, const std::string& s2)tells us what the argument actually are.For example, say we have an object
str_dist_ obj;, and twostd::stringobjects,str1andstr2. Then we can write something like thisUnder the covers, the previous line is identical to
Notice that the first set of parenthesis are part of the name of the member function
operator(), while the second set actually lists the arguments.If you want more information, look up Functors or Function Objects. These are the terms used to refer to things such as
str_dist_objects, which have theoperator()defined for them. If you google either of those terms, you’re sure to find a lot of information.