C ++ Functions

Source: Internet
Author: User
I feel totally depressed. I 've got a lot of syntaxes, but I haven't heard of them yet. I really saw an example of the observer pattern. It's really depressing to see an imitation function. I can't find the following content. dizzy, but it's better to understand that the point-to-point imitation function is to reload the function in the class, where we can have parameters. When we call this function, we can directly add the function with the class name. the following parameters can be used. In this way, it is a way to directly call a function. This is called a function imitation. I don't know who is calling it. This is troublesome. Some people just use it to show how to understand it. many of them are really depressing. The following is a specific example: //**
// * The so-called functor function is a class that simulates function behavior through the overload () operator.
// * Therefore, two points need to be clarified here:
// * 1 the imitation function is not a function. It is a class;
// * 2 simulate the function to overload the () operator so that it can be called like a function (the code is like calling a function ).
// **/# Include <iostream> // standard input/output stream
Using namespace STD; // standard namespace const int cmp_les =-1; // defined constant
Const int cmp_equ = 0;
Const int cmp_big = 1; Class comparer // The main function of the class function of the imitation function is to overload the operator () so that when calling it, You can directly use the class reference and add the parameters following ().
{
Public:
Comparer (INT cmptype)
{
M_cmptype = cmptype; // initialization variable
} 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;
}; // Exchange Values
Void swap (Int & num1, Int & num2)
{
Int temp = num1;
Num1 = num2;
Num2 = temp;
} // This Is A sort function that sorts the order in a double loop.
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]) // here is a reference to the function-like class.
{
Indx = J;
}
}
If (indx! = I)
{
Swap (array [I], array [indx]);
}
}
} // This is the output display function.
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 }; // define an array with ten data cout <"The initial array is:"; // first, output the current array data
Listarray (array, ary_size );
Cout <Endl; sortarray (array, ary_size, comparer (cmp_big )); // then the sortarray sorting function is used here. The third parameter is a // object reference, so you need to initialize this object (the comparer object is initialized first and then acted)
Cout <"the ascending sorted array is :";
Listarray (array, ary_size );
Cout <Endl; sortarray (array, ary_size, comparer (cmp_les); // sort by this method
Cout <"the descending sorted array is:"; // method after sorting is output
Listarray (array, ary_size );
Cout <Endl;
Return 0;
}//************************************* **************************************** * The program defines a function like comparer, it 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 voidSortarray(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 exactly the same as those used in the program. If you replace the program, it works normally.

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.