Standard library function object, such as. Example _to <type>
Adapter eg. bind2nd (less_equal <int> (), 10)
Not1 (bind2nd (less_equal <int> (), 10 ))
// Function adapter # include "head. H "int main () {// Binder: bind1st, bind2nd; bind a ready-made real parameter to the first or second parameter of the binary function object function, convert it to the int A [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; STD :: vector <int> VEC (A, A + 10); STD: cout <count_if (VEC. begin (), VEC. end (), STD: bind2nd (STD: less_equal <int> (), 7) <STD: Endl; // less_equal <type> does not support STD :: string type // negator: not1, not2; returns the true values of The unary function objects or binary function objects. // you can use STD :: cout <count_if (VEC. begin (), VEC. end (), not1 (STD: bind2nd (STD: less_equal <int> (), 7) <STD: Endl ;}
Pe14_37.cpp,
, 39 are compatible
(Mark) What does question C mean? Requirements
// Use the standard library function object and function adapter to define an object for: (a), (B), (c) // It is not necessary for an object to complete three functions (terrible, heavy load ?), Three questions ~! # Include "head. H "int main () {// A) searches for all values greater than 1024 int A [] = {10000, 2, 3, 4, 5, 6, 1024, 8, 9, 1025}; STD: vector <int> VEC (A, A + 10); // defines an object, greater is -- function object STD :: cout <count_if (VEC. begin (), VEC. end (), STD: bind2nd (STD: greater <int> (), 1024) <STD: Endl; STD: cout <count_if (VEC. begin (), VEC. end (), STD: bind2nd (STD: greater_equal <int> (), 1025) <STD: Endl; STD: cout <count_if (VEC. begin (), V EC. end (), STD: not1 (STD: bind2nd (STD: less_equal <int> (), 1024) <STD: Endl; STD :: cout <count_if (VEC. begin (), VEC. end (), STD: not1 (STD: bind2nd (STD: less <int> (), 1025) <STD: Endl; // B) search for all strings not equal to pooh // find_if () to find and print? Count_if () calculates the quantity? STD: vector <STD: String> SVEC (3, "world"); SVEC. push_back ("hello"); SVEC. push_back ("Pooh"); STD: String compare = "Pooh"; // Method 1: bind2nd binds not_1__to and uses count_if to count STD :: cout <"the number of strings not Sames to" <compare <":" <count_if (SVEC. begin (), SVEC. end (), STD: bind2nd (STD: not_1__to <STD: String> (), compare) <STD: Endl; // Method 2: bind2nd is used to bind performance_to and the "Pooh" String object to be searched. The outer layer of not1 can only be not 1. Here, the standard library algorithm transmits an object to evaluate the bool value. It can only call the unary function object STD :: cout <"the number of strings Sames to" <compare <":" <count_if (SVEC. begin (), SVEC. end (), STD: not1 (STD: bind2nd (STD: pair _to <STD: String> (), compare) <STD: Endl; // return the result of not1, or use logical_not <type> // C) to multiply all values by 2 // What algorithm should I use to traverse and use count_if to repeat it, multiply the value of multiplies <type> by 2? No ~!!!! // In theory, parameters are passed for specified operations. The standard library algorithm only transmits the real parameters. If any operation is used in the algorithm, no matter which operation is used, the multiplies does not say that the parameter is not allowed to be passed. // error: even if the operation can be performed, the operation may be performed on the copy, and the reference and copy cannot be returned to change the original value... Find_if is even more boring. It is just the standard library algorithm that needs to pass a function to find the position to return to the iterator. In fact, there are few issues ~! The bool type returned for judgment is invalid, int multi = 2; STD: cout <count_if (VEC. begin (), VEC. end (), STD: bind2nd (STD: multiplies <int> (), Multi) <STD: Endl; // I believe this is what he wants (maybe a overload is required to handle doubl or something like that ?): // Similar to STD: plus <int> intadd; defines an object. You do not have to multiply the elements in a container by 2, who calls it and who times 2 (question a and question B is obviously for the entire container) // STD: bind2nd (STD: multiplies <int>, multi) intmulti2; // However, this statement cannot be used. // print the verification for (STD: vector <int >:: iterator iter = Vec. begin (); iter! = Vec. End (); ITER ++) {STD: cout <* ITER <"\ t ";}}