Standard Library Function Object,eg.equal_to<type>
Adapter eg. bind2nd (Less_equal<int> (), 10)
NOT1 (bind2nd (less_equal<int> (), 10))
function adapter for function object
#include "head.h"
int main () {
//binder binder:bind1st,bind2nd; bind a ready-made argument to a two-function object function first or second argument, Turn it into a unary function
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten};
Std::vector<int> Vec (A, A +);
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
//negation negator:not1,not2; negation//negation of the value of a unary function object or a two-dollar function object
and the nesting use
of bindings Std::cout << count_if (Vec.begin (), Vec.end (), Not1 (std::bind2nd (std::less_equal<int> (), 7)) << STD :: Endl;
}
Pe14_37.cpp,
38,39 are all compatible.
(mark) The meaning of the C question. Requirements
Using standard library function objects and function adapters, define an object for: (a), (b), (c)///not to have an object complete with three functions (too scary, overloaded.)
), is three small problem ~. #include "head.h" int main () { //a) finds all values greater than 1024 int a[] = {10000, 2, 3, 4, 5, 6, 1024
, 8, 9, 1025};
std::vector<int> Vec (A, A + 10); //The so-called definition of 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 (), Vec.end (), Std::not1 (std::bind2nd
Int> (), 1024)) << Std::endl; std::cout << count_if (Vec.begin (), Vec.end (), Std::not1 (std::bind2nd (
) << Std::endl; 1025)); //b) Find all strings that are not equal to Pooh //find_if () to 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 One, bind2nd binding not_equal_to, with count_if count std::cout << "the number of Strings not sames to "<< compare <<": " <<cou Nt_if (Svec.begin (), Svec.end (), std::bind2nd (std::not_equal_to<std::string> (), compare))
<< Std::endl; //Method Two: Bind equal_to with bind2nd and to find string Object "Pooh", outer not1, //here can only not1, Here is the standard library algorithm to pass an object in to seek bool value, can only be called a unary function object std::cout << "The number of strings sames to" << compar E << ":" << count_if (Svec.begin (), Svec.end (), STD :: Not1 (std::bind2nd) (Std::equal_to<std::string> (), COmpare)) << std::endl;//not1 reverse, or with logical_not< type> //c) Multiply all values by 2 //What algorithm should I use to traverse, use count_if over, and then multiply multiplies<type> by 2.
no~!!!! //is supposed to be passed parameters to specify the operation Ah, the standard library algorithm just pass the actual parameters, which operation inside him to leave me, multiplies also did not say not allowed to pass // Error: Because even if you can do the operation, may be to the copy of the operation, and can not return the reference and the original value ... Find_if, is looking for position return iterator //now can think of, only need to pass a function of the standard library algorithm, actually did not touch a few ~.
The return type of bool used for judgment is not valid, int multi = 2; std::cout << count_if (Vec.begin (), Vec.end (), std::bind2nd (Std::multiplies<int> (),
Multi)) << Std::endl; //I'm more sure that's what he wants. (It may take a heavy load to fix DOUBL and the like.) ): //similar to std::p lus<int> intadd; Define an object without requiring that the element in a container be multiplied by 2, and who calls it multiplied by 2 (A, B Two questions are obviously about the entire container) //std::bind2nd (STD::MULTIPLIES<INT> multi) intmulti2;//But this statement doesn't come out //Print Validation for (Std::vector<int>::iteraTor iter = Vec.begin (); ITER!= vec.end (); iter++) { std::cout << *iter << "\ t"; }
}