EECS 280 Lecture 19: Function Objects

68 views4 pages

Document Summary

Syndrome bool any_odd(const forward_list &fl) { for(forward_list::iterator i = fl. begin(); i == fl. end(); ++i) { return false; if( ! (*i % 2 != 0)) { return true; Solution #2 list for(auto i:fl) { if (is_odd(i)) return true; //didn"t have to dereference. //because i is a copy of the item in the return false; In the case of any_even: if( (i % 2) == 0) return true; for(auto i:fl) { return false; Range for is automatically dereferencing for us basically; it also automatically calls the programer"s begin and end functions. If you want more control over where to start/stop, use the normal iterator. We need function pointers to do this. Let"s make a new function that can do the work of both: We need to pas the behavior o odd and even as an input to any_of. We basically want the function to behave like a variable.