C ++ Functions)

Source: Internet
Author: User

The so-called functor is a class that simulates the function form through the overload () operator.
Therefore, we need to clarify two points:
1. A function is a class instead of a function;
2. the imitation function reloads the () operator so that it can be called like a function (the code is like calling

Function ).

See the following example:

# Include <iostream>
Using namespace STD;

Const int cmp_les =-1;
Const int cmp_equ = 0;
Const int cmp_big = 1;

Class comparer
{
Public:
Comparer (INT cmptype)
{
M_cmptype = cmptype;
}

Bool
Operator () (INT num1,
Int num2) const
{
Bool res;

Switch (m_cmptype)
{
Case cmp_les:
Res = num1 <num2;
Break;
Case cmp_equ:
Res = num1 = num2;
Break;
Case cmp_big:
Res = num1> num2;
Break;
Default:
Res = false;
Break;

}

Return res;
}

PRIVATE:
Int m_cmptype;
};

Void swap (Int & num1, int
& Num2)
{
Int temp = num1;
Num1 = num2;
Num2 = temp;
}

Void sortarray (INT array [], int size, const comparer
& CMP)
{
For (INT I = 0; I <size-1; ++ I)
{
Int indx = I;

For (Int J = I + 1; j <size; ++ J)
{
If (CMP (array [indx], array [J])
{
Indx = J;
}
}

If (indx! = I)
{
Swap (array [I], array [indx]);
}
}
}

Void listarray (INT array [], int size)
{
For (INT I = 0; I <size; ++ I)
{
Cout <array [I] <"";
}
}

# Define ary_size 10

Int main ()
{
Int array [ary_size] = {10, 12, 9, 31, 93, 34, 98, 9, 1, 20 };

Cout <"The initial array is :";
Listarray (array, ary_size );
Cout <Endl;

Sortarray (array, ary_size,Comparer (cmp_big ));
Cout <"the ascending sorted array is :";
Listarray (array, ary_size );
Cout <Endl;

Sortarray (array, ary_size,Comparer (cmp_les));
Cout <"the descending sorted array is :";
Listarray (array, ary_size );
Cout <Endl;

Return 0;
}

Running result:

The initial array is: 10 12 9 31 93 34 98 9 1 20
The ascending sorted array is: 1 9 9 10 12 20 31 34 93 98
The descending sorted array is: 98 93 34 31 20 12 10 9 9 1

The program defines the comparer function, which reloads the () OPERATOR:
Comparer: bool Operator
() (INT num1,
Int num2) const;
Here we will review the method of Operator Overloading:
Ret_type Operator
Opt (array_list );
Here, ret_type is the type of the returned value after the operator is overloaded, operator is the c ++ operator, and OPT is the operator to be overloaded, such as +,-, *,/, [], ()...
So we can interpret the meaning of comparer: bool operator () (INT num1, int num2) const:
Bool limits the return value of () to the boolean type. (INT num1, int num2) specifies the parameter form of the operator (), so that the operator can be called by its const object. () Operator returns the comparison values of two integers in different ways based on the m_cmptype value.

Function void
Sortarray(INT array [],
Int size, const
Comparer & CMP) is used to sort arrays. Here, array [] specifies the array object to be sorted. Size specifies the number of array elements. CMP is a reference to the comparer object and is used for element comparison, previously, the const modifier is declared to all function calls. No data of this object is modified in the function. Note the code in sortarray:
If (CMP (array [indx], array [J])
{
Indx = J;
}
CMP is an object of the comparer class, but its usage seems to be like a function. This is the true meaning of the function.

Void swap (Int & num1, Int & num2) can exchange num1 and num2 values. Int & num1 indicates the reference of function parameters. If you have been using C for a long time, you may be more accustomed to void swap (int * num1, int * num2 ), but in C ++, this habit has to be changed. The reference is as efficient as the pointer, but the reference is more intuitive than the pointer. The following is the swap function of pointer edition:
Void swap (int * num1, int * num2)
{
Int temp = * num1;
* Num1 = * num2;
* Num2 = temp;
}
The implemented functions are the same as those used in the program, and the replacement of the program works normally. After carefully comparing the swap () Functions of the reference version and pointer version, I believe most people will fall in love with the c ++ reference version.

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.