Chapter XVII - Under the function

Based on book learning C ++. On the front of the function of the composition, definitions, declarations, calling not talk about it. Here talk and the reference parameters from a pointer portion.

Function value has been passed way transmission parameters, but transmission value of the argument, the argument itself instead. On the one hand there will be a problem to do so efficient, because for a large data types assignment process; the other hand, does not change the value of argument in a function, because function is only to build a copy of the argument. With pointers and references as an argument, you can make up for these shortcomings.

A pointer parameter

Function in the parameter list, the parameter pointer type may be used. Argument to the argument pointer may be a pointer variable, or the address of a variable. For example, the following parameters of the function is a pointer of type int:

void function(int *);

Also must pass a pointer to an int variable function call function. E.g:

int * p;                      // define a pointer variable of type int 
.....                        // certain operation of changing the value of p 
function (p);                 // to p as arguments, the function call function 
int iVal = 0 ;                // defined integer variable 
function (& iVal);             // get the address of integer variables, the pointer passed to the function and form of
View Code

 

Use pointer as an argument passed by value can compensate for the lack of:

  1. improve the efficiency of transmission parameters

  2. The argument function may modify the value of the pointer variable

For larger data type, defined, for example a lot of data structure members, 100 integers is assumed members. If such a structure variable passed as a parameter, then the construct also function inside a structure similar variable, and an assignment of its members. Thus in memory will have the same structure of two variables, occupies 800 (200 * 4) bytes of memory. This is both in space and time is a huge waste. Class as well. In fact, in C ++, the difference in the structure of the class is small.

If a pointer is passed, it is possible to eliminate waste in space and time to bring the value passed. This is mainly because the pointer value is an address value (the number is a 4-byte integer in 32-bit system), only the address value can be transmitted when a parameter.

Tip: pointer as a parameter, the value passed is the principle of compliance. Value of the actual value of the pointer is passed, instead of the value of the pointer variable.

For example, for such a large structure SomeStruct, with the pointer as a parameter can be written as:

void function(SomeStruct *p);

And when you call the function function can be written with SomeStruct variables as parameters as follows:

SomeStruct a;         // define a variable SomeStruct 
......                // define a variable operating 
SomeStruct * P = & a;   // define a SomeStruct type pointer to a variable the above (address value acquired a) 
function ( P);          // with SomeStruct type of pointer as an argument, the function calls the function
View Code

 

So when you call the function, the function can be constructed only copy of a pointer variable, rather than constructed copy of the entire structure, saving memory, and eliminates the need for time to each data member assignment, but also greatly improve the efficiency of time . Pointer as an argument with another advantage is that the function can modify the pointer indicated value of the variable argument. This is because the parameters are transferred through a pointer address of a variable, the variable may be obtained by the address within the function, so that it can modify the address of the variable. E.g:

void function ( int * P)    // defined function function 
{
     * P = 123 ;            // get a pointer to a variable parameter referred to, and assigned to the variable 
} 
...... 
int A = 0 ;               // define integer variables a, and is initialized to 0 
function (& a);            // the address to a variable transfer function function 
COUT << a;               // value of the output variable a
View Code

 

The above-described program outputs 123, rather than 0. This is because the internal function of a variable value changed. And if the argument of type int, then reached this effect. The following example defines a function for switching the value of two variables. A common type due to the use of the parameter value of the argument can not be modified, it is possible to use the pointer as an argument.

void the swap ( int * a, int * b) {
     int TEMP * = a;                             // save a pointer variable with a temporary variable value 
    * = a * b;                                   // b with the value assigned to the variable pointer is a pointer variable 
    * b = TEMP;                                 // the value of the temporary variable is assigned to the variable b pointer 
} 

int main ( int argc, char * the argv []) { 
    COUT << " - pointer parameter - " << endl;             // output message 
    cout << " enter two integers: " <<endl;
     int A = 0 , B = 0 ;                          // save the user variable input 
    CIN >> A;                                  // input 
    CIN >> B; 
    the swap ( & A, & B);                              // call to the swap function 
    COUT << " exchange after: " << a << " \ T " << endl;    // value output switching 
    System ( " the PAUSE " );                           // wait for user response to 
    return of EXIT_SUCCESS; 
}
View Code

 

In the definition of the swap function, the pointer as a parameter, the parameters used to avoid the conventional type, so as to achieve the purpose of the exchange of variable values. In fact, see here, might doubt, until learned references, that can also change the value, and pointer variables like ah, right, and then go back to the quote, the quote is actually a variable entity alias. Pointer can be re-assigned to the new object reference to an object can not be changed after the initialization. And then, through the bubble sort, the two methods, to show the difference. reference compare, swap pointers

#include "pch.h"
#include <iostream>
using namespace std;

#define len 10  //define array length equal 10

//function prototype
void Method_one();
void Method_two();
void compare(int &a, int &b);
void swap(int *a, int *b);

int break_node = 0;//when break_node is true, jump the loop

int main()
{
    //Bubble sort
    //Method_one();
    Method_two();
    
}

void Method_one() {
    //define an array which length is 10
    int array_sort[len];

    //input array value
    cout << "Please input " << len << " number: " << endl;
    for (int i = 0; i < len; i++)
        cin >> array_sort[i];


    //start sort
    for (int m = len; m > 0; m--) {
        for (int j = 0; j < m - 1; j++) {
            compare(array_sort[j], array_sort[j + 1]);
        }
        if (break_node) {
            break_node = 0;
        }
        else {//while no change data in inner loop, jump out the external loop
            break;
        }
    }

    //output sorted array(high-->low)
    cout << "Sorted array: " << endl;
    for (int k = 0; k < len; k++) {
        cout << array_sort[k] << " ";
    }
    cout << endl;
}

void Method_two() {
    int length = 0;
    cout << "Please input the length of array : " << endl;
    cin >> length;
    cout << "Please input " << length << " values" << endl;
    int *pArr = new int[length];

    //input array value
    for (int i = 0; i < length; i++)
        cin >> pArr[i];

    //start sort
    for (int m = length; m > 0; m--) {
        for (int j = 0; j < m - 1; j++) {
            compare(pArr[j], pArr[j + 1]);
        }
        if (break_node) {
            break_node = 0;
        }
        else {//while no change data in inner loop, jump out the external loop
            break;
        }
    }

    //output sorted array(high-->low)
    cout << "Sorted array: " << endl;
    for (int k = 0; k < length; k++) {
        cout << pArr[k] << " ";
    }
    cout << endl;

    delete pArr;
}

void compare(int &a, int &b) {
    if (a >= b);
    else {
        //int temp;
        //temp = a;
        //a = b;
        //b = temp;
        //break_node++;
        swap(&a, &b);
        break_node++;
    }
}

//change two variable position
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
View Code

 

Guess you like

Origin www.cnblogs.com/smart-zihan/p/11295033.html