(Original callback) how to use function object? (C/C ++) (STL)

Source: Internet
Author: User

Many STL algorithm are at the end of _ if, so that we can bring the function in. If function object is used together, the function can be more coherent !!

In the following example, we want to use count_if () algorithm to know that there are more than n vector?

1 /**/ /*  
2 (C) oomusou 2007 Http://oomusou.cnblogs.com
3
4 Filename: functionobjectsimple. cpp
5 Compiler: Visual C + + 8.0/iso c ++
6 Description: Demo how to use function object
7 Release: 01/18/2007 1.0
8 */
9 # Include < Iostream >
10 # Include < Vector >
11 # Include < Algorithm >
12
13 Using   Namespace STD;
14
15 Bool Biggerthan3 ( Int );
16
17 // Function object
18 Struct Biggerthan {
19 Int N;
20 Biggerthan ( Int N): n (n) {}   // Constructor
21 Bool   Operator ()( Int Val) {ReturnVal>N ;}
22 } ;
23
24 Int Main () {
25 Int IA [] =   {1,2,3,4,5} ;
26 Vector < Int > Ivec (IA, IA +   Sizeof (Ia) /   Sizeof ( Int ));
27 Int I = Count_if (ivec. Begin (), ivec. End (), biggerthan3 );
28 Cout < I < Endl;
29 Int J = Count_if (ivec. Begin (), ivec. End (), biggerthan ( 3 ));
30 Cout < J < Endl;
31 }
32
33 Bool Biggerthan3 ( Int Val) {
34ReturnVal> 3;
35}

Results

2
2

If there is no function object, we can only bring a function name in, because the signature is fixed, so we can only bring a fixed n function as 33 rows, but STL algorithm allows us to bring function objects in. If we can use function objects, they will be very intrusive ~ In line 22, the function is wrapped into a function object. Of course, the class is also used. However, if struct is used, the public: word can be omitted. First, the constructor is used to take over the numeric value, and then () operator performs overload. In this case, 29 rows can carry forward data, regardless of the number of N bands. Of course, operator has more flexibility.

See also
(Original token) How to Use the token circle (using for_each )? (中c) (C ++) (STL) (oo c ++) (template C ++)
(Original example) how to add a line feed to a program notebook? (C/C ++) (STL)

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.