// Demonstrating use of STL's predicate templates and adaptors #include #include #include void show_int (int x) { cout << x << " "; } int main() { cout << "Demonstrating Function Objects with lists." << endl; list list2; // populating the list using "push_back" list2.push_back(89); list2.push_back(99); list2.push_back(89); list2.push_back(69); list2.push_back(64); list2.push_back(89); list2.push_back(89); list2.push_back(49); cout << "LIST2 Elements: " ; for_each(list2.begin(), list2.end(), show_int); cout << endl; // following example shows how to use pre-existing STL // templates "greater" and adaptor "bind2nd" to define a // predicate which returns true when the parameter it // is given is greater than 40... // bind2nd is called a function ADAPTOR because it // converts a binary comparison to a unary comparison by // fixing the 2nd parameter to some constant value... cout << "Count of numbers > 40: " << count_if(list2.begin(), list2.end(), bind2nd(greater(), 40)) << endl; // look for for items > 80 cout << "Count of numbers > 80: " << count_if(list2.begin(), list2.end(), bind2nd(greater(), 80)) << endl; // replace numbers less than 80 using "less" and bind2nd // as before... replace_if(list2.begin(), list2.end(), bind2nd(less(), 80), 25); cout << "LIST2 Elements (after replacement): " ; for_each(list2.begin(), list2.end(), show_int); cout << endl; }