C + + Function object

Source: Internet
Author: User

1, consider the following requirements, from the collection to find a student equal to the current student, as follows:
int main (int argc, char* argv[])
{
Student S1 ("Andy");
Student S2 ("Bill");
Student S3 ("Caroline");
Student S4 ("David");
Student S5 ("Eric");

Vector<student> Stuvec;
Stuvec.push_back (S1);
Stuvec.push_back (S2);
Stuvec.push_back (S3);
Stuvec.push_back (S4);
Stuvec.push_back (S5);

Student Target ("Bill");

Vector<student>::iterator iter = Find (Stuvec.begin (), Stuvec.end (), target);

return 0;
}
Note: Call the Find method, student to overload the member operator = =, or overload the normal operator = =, because find uses = = To compare two objects for equality.
2, the need to change, find a older than target students, how to do?
Using a Function object, the function object has a member _stu, using the target initialization _stu, overloading (), comparing the target with each object in the sequence
Class Finder
{
Public
Finder (const student& stu): _stu (Stu)
{

}

BOOL Operator () (const student& RHS)
{
if (rhs._age>_stu._age)
{
return true;
}
return false;
}

Private
Student _stu;
};

iter = find_if (Stuvec.begin (), Stuvec.end (), Finder (target));
Note: If the collection of teacher, also want the same demand, find a older than target teachers, you can change the Finder into a template class.

3. Is there any other way?
Using greater<student> can compare the size of Student, one is a variable, one is a reference, and Find_if's third parameter, the implicit interface is Pred (*first), only accept one parameter.
Using bind2nd for greater<student> and reference encapsulation, the external exposed interface accepts a parameter, calls the greater (parameter, reference) internally, Greater uses > compares the size, and overloads the > operator for Student.
BOOL Operator> (const student& lhs,const student& RHS)
{
return lhs._age > Rhs._age;
}

iter = find_if (Stuvec.begin (), Stuvec.end (), bind2nd (greater<student> (), target));

4. What are the other options? The
does not load the operator, but greater a single-bit to compare the age size. As follows:
template <>
struct greater<student>: Public binary_function<student, Student, Bool>
{
bool Operator () (const student& _left, const student& _right) const
{
return (_left._age > _right._ Age);
}
};
iter = find_if (Stuvec.begin (), Stuvec.end (), bind2nd (greater<student> (), target));
5, because there is only one special, can you write a similar greater method object, and is able to use the bind2nd adaptation.
Note: In order to be adaptable, you need to inherit public binary_function<student, Student, bool>
struct Studentnamecompare:public binary_ Function<student, Student, Bool>
{
bool operator () (const student& _left, const student& _right) Const
{
return (_left._age > _right._age);
}
};
iter = find_if (Stuvec.begin (), Stuvec.end (), bind2nd (Studentnamecompare (), target));

Of course, you can also use templates, as follows:
Template <typename t>
struct Studentnamecompare:public binary_function<t, T, bool>
{
BOOL Operator () (const t& _left, const t& _right) const
{
Return (_left._age > _right._age);
}
};
iter = find_if (Stuvec.begin (), Stuvec.end (), bind2nd (studentnamecompare<student> (), target));

6, now analyze 2 methods and 5 methods, the final interface to meet Pred (*first), only accept one parameter. The processing strategies of the two are different. In 2, the function object accepts only one parameter, but there is a field that is used to hold the reference. In 5, the function object accepts two parameters, inherits the Binary_function, makes it adaptable, and then encapsulates the function object and the reference using bind2nd, and the externally exposed interface accepts only one parameter.
7, consider the following needs, find a younger than target students, for 5, only need to use the not1 once again to fit, as follows:
iter = find_if (Stuvec.begin (), Stuvec.end (), Not1 (bind2nd (studentnamecompare<student> (), target));
But for 2, no. Since 2 is not adaptable, to allow 2 to be adaptable, you need to inherit the public Unary_function<student,bool>, as follows:
Class Finder:public Unary_function<student,bool>
{
Public
Finder (const student& stu): _stu (Stu)
{

}

BOOL Operator () (const student& RHS) const
{
if (rhs._age>_stu._age)
{
return true;
}
return false;
}

Private
Student _stu;
};
Of course, you can also use templates.

C + + Function object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.