In a book (Sec 6.6.5 of Jossoutis' book on the Standard Library), I find this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*function object to check the value of a map element
*/
template <class K, class V>
class value_equals {
private:
V value;
public:
//constructor (initialize value to compare with)
value_equals (const V& v)
: value(v) {
}
//comparison
booloperator() (pair<const K, V> elem) {
return elem.second == value;
}
};
which is invoked in the following way:
1 2 3
//search an element with value 3.0
pos = find_if (coll.begin(),coll.end(), // linear complexity
value_equals<float,float>(3.0));
Can you explain why you call it this way?
How is the method "operator" invoked when you call the constructor "value_equals"?
Also, I do not understand the syntax of the constructor...why is there an & after the V in the definition of the constuctor?
It also seems to have an empty body. Could someone explain that?
I should have made clear that I understand the basic use of "find_if", as in the example you provided, where the third argument is a Boolean function. The part I don't understand is how one interprets the class definition in the first block so that:
value_equals<float,float>(3.0)
is a Boolean function. The fact that the above is used inside a find_if command is really incidental to my question...I hope this clarifies my question.
Another way of putting it is that as far as I can see the ()'s around the 3.0 in the above line of code are part of the call to the class constructor value_equals. But there also must be ()'s that are interpreted as the overloaded ()'s defined in the method definition