STL source code analysis-STL function objects

Source: Internet
Author: User
Tags modulus
Preface

Function objects are also important in STL, and sometimes the behavior of STL algorithms can be limited. For example, in the STL algorithm analysis described earlier, each algorithm basically provides two operation versions, one of which allows you to specify the function object. This allows you to operate the algorithm based on your needs. Function objects are functional objects that can be used as algorithm parameters. The function objects described in this article are simple. They are arithmetic function objects, relational operation class function objects, and logical operation class function objects based on the one-or binary operation structure. When defining a function object, the operator () operator must be overloaded to make it have function behavior. The source code of this article is from the <stl_functional.h> file in sgi stl.

Function object source code analysis

_ Stl_begin_namespace // One-dimensional operation structure definition template <class _ Arg, class _ result> struct unary_function {typedef _ Arg argument_type; // parameter type typedef _ result result_type; // return result type}; // binary operation structure definition template <class _ arg1, class _ arg2, class _ result> struct binary_function {typedef _ arg1 first_argument_type; // parameter 1 type typedef _ arg2 second_argument_type; // parameter 2 type typedef _ result result_type; // return result type}; // The following is a binary operation arithmetic function object, inherit binary operation binary_fu Nction structure/* addition operation plus <t>, subtraction operation minus <t>, multiplication operation multiplies <t>, division operation divides <t>, modulo operation modulus <t>, */template <class _ TP> struct plus: Public binary_function <_ TP, _ TP, _ TP> {_ TP operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x + _ y ;}}; template <class _ TP> struct minus: public binary_function <_ TP, _ TP, _ TP> {_ TP operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x-_ y ;}}; template <class _ TP> struct multiplies: Public binary_function <_ TP, _ TP, _ TP> {_ TP operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x * _ y; }}; template <class _ TP> struct divides: Public binary_function <_ TP, _ TP, _ TP >{_ TP operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x/_ y ;}}; template <class _ TP> struct modulus: Public binary_function <_ TP, _ TP, _ TP> {_ TP operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x % _ y ;}}; // One-dimensional operation, inherit the unary_function structure of a one-dimensional operation // negative value operation negate <t> template <class _ TP> struct negate: Public unary_function <_ TP, _ TP> {_ TP operator () (const _ TP & _ x) const {return-_ x ;}}; // identity_element (not part of the C ++ standard ). // verify the same element: // only the following two types of verify the same element are provided. // addition: Add 0 to any element and the result is self // multiplication: any element multiplied by 1 is its own template <class _ TP> inline _ TP identity_element (plus <_ TP>) {return _ TP (0);} template <Class _ TP> inline _ TP identity_element (multiplies <_ TP>) {return _ TP (1) ;}// The following is a binary operation relation function object, the structure that inherits binary operations/* the return value type is bool equal_to, not_equal_to, greater, less, greater_equal, less_equal, */template <class _ TP> struct defaults _to: public binary_function <_ TP, _ TP, bool> {bool operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x ==__ y ;}; template <class _ TP> struct not_to _to: Public binary_function <_ TP, _ t P, bool> {bool operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x! = _ Y ;}}; template <class _ TP> struct greater: Public binary_function <_ TP, _ TP, bool> {bool operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x> _ y ;}}; template <class _ TP> struct less: public binary_function <_ TP, _ TP, bool> {bool operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x <_ y ;}}; template <class _ TP> struct greater_equal: Public binary_function <_ TP, _ TP, bool> {bool Functions Ator () (const _ TP & _ x, const _ TP & _ y) const {return _ x >=_ _ y ;}}; template <class _ TP> struct less_equal: Public binary_function <_ TP, _ TP, bool> {bool operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x <= _ y ;}}; // The following is the binary operation logic function object, inherit the structure of binary operations/* Where logical_not is the one-dimensional operation function logical_and, logical_or, logical_not */template <class _ TP> struct logical_and: Public binary_function <_ TP, _ TP, bool> {bool Operator () (Const _ TP & _ x, const _ TP & _ y) const {return _ x & _ y ;}}; template <class _ TP> struct logical_or: Public binary_function <_ TP, _ TP, bool> {bool operator () (const _ TP & _ x, const _ TP & _ y) const {return _ x | _ y ;}}; template <class _ TP> struct logical_not: Public unary_function <_ TP, bool> {bool operator () (const _ TP & _ x) const {return! _ X ;}; // identity is an extensions: it is not part of the standard. // verify the function. // no modification is made after any value passes through this function. Therefore, if the return value type is const, reference template <class _ TP> struct _ identity: Public unary_function <_ TP, _ TP> {const _ TP & operator () (const _ TP & _ x) const {return _ x ;}}; template <class _ TP> struct identity: Public _ identity <_ TP> {}; // select1st and select2nd are extensions: they are not part of the standard. // select function // version 1: select the first parameter of the pair element and ignore the second parameter template <class _ pair> struct _ select1st: Public unary_function <_ pair, typename _ pair: first_type> {const typename _ pair: first_type & operator () (const _ Pair & _ x) const {return _ x. first ;}}; // version 2: select the second parameter of the pair element and ignore the first parameter template <class _ pair> struct _ select2nd: Public unary_function <_ pair, typename _ pair: second_type> {const typename _ pair: second_type & operator () (const _ Pair & _ x) const {return _ x. second ;}}; template <class _ pair> struct select1st: Public _ select1st <_ pair >{}; template <class _ pair> struct select2nd: public _ select2nd <_ pair >{}; // project1st and project2nd are extensions: they are not part of the standard // Projection Function // version 1: The first parameter is specified, ignore the second parameter template <class _ arg1, class _ arg2> struct _ project1st: Public binary_function <_ arg1, _ arg2, _ arg1> {_ arg1 operator () (const _ arg1 & _ x, const _ arg2 &) const {return _ x ;}; // version 2: The second parameter is specified, ignore the first parameter template <class _ arg1, class _ arg2> struct _ project2nd: Public binary_function <_ arg1, _ arg2, _ arg2> {_ arg2 operator () (const _ arg1 &, const _ arg2 & _ y) const {return _ y ;}; template <class _ arg1, class _ arg2> struct project1st: public _ project1st <_ arg1, _ arg2 >{}; template <class _ arg1, class _ arg2> struct project2nd: Public _ project2nd <_ arg1, _ arg2> {};

References:

STL source code analysis Hou Jie

STL source code analysis-STL function objects

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.