Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to reference arrays in C++
How to reference arrays in C++

Yes, you can use the "reference to the array" as a function parameter - that is, pass the entire array to the function,

There is no need to use the array length as the second parameter.

The specific implementation is as follows:

//Using template implementation, the Print function can print any length array of any basic type.

template

void Print(TYPE (&arr)[SIZE]){

for(int i=0;i< SIZE;++i){

cout<<*(arr+i)<<" ";

}

cout<

}

int main(){

int iarr[]={1,2,3,4,5};

float farr[]={1.1, 5.5,9.9};

string sarr[]={"first","second"};

Pirnt(iarr);

Print(farr);

Print(sarr);

return 0;

}

//--- --------------------------------------------------< /p>

Output results:

1, 2, 3, 4, 5

1.1, 5.5, 9.9

first second