Implementation of STL simple function object (imitation function)

Source: Internet
Author: User
Tags modulus

1. Introduction

This article describes something that has two different names in STL history. The functor (functors) is an early name, and after the C + + standard is finalized, the new name used is the function object (Functions objects).

The function object, as the name implies, is first the object (as we can see later, the function object is defined by a struct rather than a class), and secondly, we can invoke the function object as if it were called a function. This is like a function pointer, unfortunately, the function pointer does not meet the object-oriented requirements, the STL does not meet the requirements of the template abstraction, and function pointers can not be used in conjunction with other STL components. So the clever scientists play a fantastic role, wrapping a layer of objects into the naked function, and overloading the operator () method, writing the implementation of the function into operator () so that we can pass the function as an object, is it witty?

In terms of implementation, a function object is actually an object that behaves like a function. In order to be able to "behave like a function", the operator () operator must be overloaded within the object, and then we can add a pair of parentheses after the function object to invoke the operator () defined by the function object.

The relationship between the function object and the algorithm is as follows:

2. Design and implementation

The program I wrote with VS2013 (GitHub), the implementation version of the function object is located at cghstL/version/ Cghstl-0.6.5.rar

The function object described in this article requires the following files:

1. Cghstl_funcbase.h, which contains the base class of the function object, all function objects must inherit from the base class, located at cghstl/function Objects/

2. Cgh_function_obj.h: The main character of this article, including the arithmetic class function object, the relational operation class function object, the logical operation class function object implementation, is located in cghstl/functionobjects /

3. Test_relational_function_obj.cpp, test relational Operation class function object, located at cghstl/test/

4. Test_logical_function_obj.cpp, test logical Operation Class function object, located at cghstl/test/

5. Test_arithmetic_function_obj.cpp, Test arithmetic class function object, located at cghstl/test/;

2.1 Base class implementations of Function objects

STL function objects should be capable of being decorated with function adapter and strung together like bricks. In order to have the mating capability, each function object must define its own corresponding type (associative types), as if the iterator were to be integrated into the STL family, and must define its own 5 types as defined. These types are intended to allow the adapter to remove information from the function object. Type definitions are only a few typedef, and all operations are completed at compile time, with no effect on the efficiency of the program run time.

The type of the function object is primarily used to represent the function parameter type and the return value type. In Cghstl_funcbase.h (cghstl/functionobjects/), we defined two classes, Represents the base class of a unary function object and a two-tuple function object, respectively. Any function object must inherit from one of the two base classes, and as long as the base class is inherited, it has a type and has the mating capability.

Cghstl_funcbase.h defines the base class of the Function object (two total), the code is as follows

/********************************************************************  Copyright (c) Chen gonghao* all  Rights reserved.**  [email protected]**  File contents: Arithmetic Class (arthmetic) imitation function implementation **************************************** /#ifndef _cgh_stl_func_base_h_#define _cgh_stl_func_base_h_namespace Cgh{/*unary_ function is used to render the parameter type and return value type of a unary function STL rules, each adaptable unary function should inherit unary_function*/template<class arg, class Result>struct unary_function{typedef argargument_type;typedef resultresult_type;};/ *binary_function is used to render the first parameter type of the two-tuple function, the second parameter type, the return value type STL rules, each adaptable binary function should inherit binary_function*/template <class arg1, Class Arg2, class Result>struct binary_function{typedef arg1first_argument_type;typedef arg2second_ Argument_type;typedef resultresult_type;};} #endif


2.1 Implementation of Function objects

The cgh_function_obj.h contains three types of function objects: arithmetic class, relational operation class, logical operation class.

The design of the function object is simple, the question place has given the comment, the code is as follows

/******************************************************************** Copyright (c) Chen gonghao* All rights reserved.** [email protected]** File contents: Implementation of Function Object *************************************************************** /#ifndef _cgh_function_obj_h_#define _cgh_function_obj_h_#include "cghstl_funcbase.h" namespace cgh{#pragma Region arithmetic class (arithmetic) function object/* Add */template<class t>struct plus:public binary_function<t, T, t>{t operator () (const t& x, const t& y) Const{return x + y;}};/ * Subtract */template<class t>struct minus:public binary_function<t, T, t>{t operator () (const t& x, const T&AM P Y) Const{return x-y;}};/  * Multiply */template<class t>struct multiplies:public binary_function<t, T, t>{t operator () (const t& x, const t& y) Const{return x * y;}};/ * Divide */template<class t>struct divides:public binary_function<t, T, t>{t operator () (const t& x, const T& Amp Y) Const{return x/y;}};/ * To seek the remainder */template<class T>struct modulus:public binary_function<t, T, t>{t operator () (const t& x, const t& y) Const{return x% y;}};/ * Take anti-*/template<class t>struct negate:public unary_function<t, t>{t operator () (const t& x) Const{return -X;}}; #pragma endregion#pragma Region relational operations Class (relational) function object/* equals */template<class t>struct equal_to:public binary_ Function<t, T, Bool>{bool operator () (const t& x, const t& y) Const{return x = = y;}};/ * Not equal to */template<class t>struct not_equal_to:public binary_function<t, T, Bool>{bool operator () (const T&AMP ; X, const t& y) Const{return x! = y;}};/ * Greater than */template<class t>struct greater:public binary_function<t, T, Bool>{bool operator () (const T& x, CO NST t& y) Const{return x > y;}};/  * Less than */template<class t>struct less:public binary_function<t, T, Bool>{bool operator () (const t& x, const t& y) Const{return x < y;}};/ * Greater than or equal to */template<class t>struct grEater_equal:public binary_function<t, T, bool>{bool operator (const t& x, const t& y) Const{return x > = y;}};/  * Less than or equal to */template<class t>struct less_equal:public binary_function<t, T, Bool>{bool operator () (const T& X, const t& y) Const{return x <= y;}}; #pragma endregion#pragma Region logical operation Class (logical) function object/* Logic and */template<class t>struct logical_and:public Binary_ Function<t, T, Bool>{bool operator () (const t& x, const t& y) Const{return x && y;}};/ * Logic or */template<class t>struct logical_or:public binary_function<t, T, Bool>{bool operator () (const T& x , const t& y) Const{return x | | y;}};/ * Logical non-*/template<class t>struct logical_not:public unary_function<t, Bool>{bool operator () (const T& x) c Onst{return!x;}}; #pragma endregion} #endif


3. Test the 3.1 arithmetic class function object

The main contents of the test link are explained in the comments

/******************************************************************** Copyright (c) Chen gonghao* All rights reserved.** [email protected]** file contents: Test arithmetic Class (arthmetic) function object (faux function) ********************************************* /#include "stdafx.h" #include <iostream> #include "cgh_function_obj.h" int _tmain (int argc, _tchar* argv[]) {using namespace::cgh;plus<int> plusobj;minus<int> minusobj;multiplies<int> multipliesobj;divides<int> dividesobj;modulus<int> modulusobj;negate<int> NagateObj;std::cout << std::endl;std::cout << "Arithmetic class function Objects Total 5: Plus, minus, multiplies, divides, modulus, negate" << Std::endl << std::endl;std::cout << "************************ Test arithmetic Class function object 1 ***************************" << std: : Endl << std::endl;std::cout << "Plusobj (3, 5) =" << plusobj (3, 5) << Std::endl << std::end L;std::cout << "Minusobj (3, 5) =" << minusobj (3, 5) << Std::eNDL << std::endl;std::cout << "Multipliesobj (3, 5) =" << multipliesobj (3, 5) << Std::endl <&lt ; Std::endl;std::cout << "Dividesobj (3, 5) =" << dividesobj (3, 5) << Std::endl << Std::endl;std::co UT << "Modulusobj (3, 5) =" << modulusobj (3, 5) << Std::endl << std::endl;std::cout << "Naga Teobj (3) = "<< nagateobj (3) << Std::endl << std::endl;std::cout <<" ************************* test Arithmetic class function Object 2 ************************** "<< Std::endl << std::endl;std::cout <<" plus<int> () (3, 5) = "<< plus<int> () (3, 5) << Std::endl << std::endl;std::cout <<" minus<int> () (3, 5) = "<< minus<int> () (3, 5) << Std::endl << std::endl;std::cout <<" multiplies<int> () (3 , 5) = "<< multiplies<int> () (3, 5) << Std::endl << std::endl;std::cout <<" DIVIDES&LT;INT&G t; () (3, 5) = "<< divIDEs<int> () (3, 5) << Std::endl << std::endl;std::cout << "modulus<int> () (3, 5) =" << mo Dulus<int> () (3, 5) << Std::endl << std::endl;std::cout << "negate<int> () (3) =" << NE Gate<int> () (3) << Std::endl << std::endl;system ("pause"); return 0;}

Test results:


3.2 Test of relational Operation class function object

The main contents of the test link are explained in the comments

Test_relational_function_obj.cpp

/******************************************************************** Copyright (c) Chen gonghao* All rights reserved.** [email protected]** file contents: Test Relational Operations Class (relational) function object (functor) ****************************************** /#include "stdafx.h" #include <iostream> #include "cgh_function_obj.h" int _tmain (int ARGC, _tchar* argv[]) {using namespace::cgh;equal_to<int> equal_to_obj;not_equal_to<int>not_equal_to_ obj;greater<int> greater_obj;greater_equal<int> greater_equal_obj;less<int> less_obj;less_equal <int> less_equal_obj;std::cout << std::endl;std::cout << "Relationship class function object Total 5: equal_to, Not_equal_to, Greater, \n\t\t\tgreater_equal, less, less_equal "<< std::endl << std::endl;std::cout <<" *********** Test relationship Class Function object 1 *************************** "<< std::endl << std::endl;std::cout <<" Equal_ To_obj (3, 5) = "<< equal_to_obj (3, 5) << Std::endl << std::endl;std::cOut << "Not_equal_to_obj (3, 5) =" << not_equal_to_obj (3, 5) << Std::endl << Std::endl;std::cout & lt;< "Greater_obj (3, 5) =" << greater_obj (3, 5) << Std::endl << std::endl;std::cout << "greate R_equal_obj (3, 5) = "<< greater_equal_obj (3, 5) << Std::endl << std::endl;std::cout <<" Less_obj ( 3, 5) = "<< less_obj (3, 5) << Std::endl << std::endl;std::cout <<" Less_equal_obj (3, 5) = "<& Lt Less_equal_obj (3, 5) << Std::endl << std::endl;std::cout << ************************* Test Relationship class Function Object 2 * * * "<< Std::endl << std::endl;std::cout <<" equal_to<int> () (3, 5) = "<& Lt Equal_to<int> () (3, 5) << Std::endl << std::endl;std::cout << "not_equal_to<int> () (3, 5) = "<< not_equal_to<int> () (3, 5) << Std::endl << std::endl;std::cout <<" greater<int> () (3, 5) = "<< GreaTer<int> () (3, 5) << Std::endl << std::endl;std::cout << "greater_equal<int> () (3, 5) =" &lt ;< greater_equal<int> () (3, 5) << Std::endl << std::endl;std::cout << less<int> () (3, 5 ) = "<< less<int> () (3, 5) << Std::endl << std::endl;std::cout <<" less_equal<int> () ( 3, 5) = "<< less_equal<int> () (3, 5) << Std::endl << std::endl;system (" pause "); return 0;}


Test results:


3.3 Testing of logical class function objects

The main contents of the test link are explained in the comments

/******************************************************************** Copyright (c) Chen gonghao* All rights reserved.** [email protected]** file contents: Test Logical Operation Class (Logical) function object (functor) ********************************************* /#include "stdafx.h" #include <iostream> #include "cgh_function_obj.h" int _tmain (int argc, _tchar* argv[]) {using namespace::cgh;logical_and<int> logical_and_obj;logical_or<int> logical_or_obj; logical_not<int> logical_not_obj;std::cout << std::endl;std::cout << "Logical Class Function Object 3: Logical_and, Logical_or, Logical_not "<< std::endl << std::endl;std::cout <<" ************************ Test Logic Class function object 1 "<< Std::endl << std::endl;std::cout <<" Logical_and_obj (3, 5) = "< < Logical_and_obj (3, 5) << Std::endl << std::endl;std::cout << "Logical_or_obj (3, 5) =" << log Ical_or_obj (3, 5) << Std::endl << std::endl;std::cout << "Logical_not_obj (3) =" << logical_not_obj (3) << Std::endl << std::endl;std::cout << "********** Test logic Class Function object 2 ************************** "<< Std::endl << std::endl;std::cout <<" Logical_and_obj<int> () (3, 5) = "<< logical_and<int> () (3, 5) << Std::endl << std::endl;std  :: cout << "logical_or_obj<int> () (3, 5) =" << logical_or<int> () (3, 5) << Std::endl << Std::endl;std::cout << "logical_not_obj<int> (3) =" << logical_not<int> () (3) << std:: Endl << std::endl;system ("pause"); return 0;}


Test results:


Implementation of STL simple function object (imitation function)

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.