# C++中数组作为函数参数或返回值

### 数组作为函数参数

#include <iostream>
using namespace std;
int sum(int arr[]){
int length = sizeof(arr)/sizeof(arr[0]);
int sum = 0;
for (int i = 0 ; i < length; i++){
sum += arr[i];
}
return sum;
}
int main(){
int arr[] = {1,3,5,7,9};
int summary = sum(arr);
cout << summary << endl;
}


#include <iostream>
using namespace std;
int sum(int* arr){
int length = sizeof(arr)/sizeof(arr[0]);
int sum = 0;
for (int i = 0 ; i < length; i++){
sum += arr[i];
}
return sum;
}
int main(){
int arr[] = {1,3,5,7,9};
int summary = sum(arr);
cout << summary << endl;
}

#include <iostream>
using namespace std;
int sum(int* arr,int length){
int sum = 0;
for (int i = 0 ; i < length; i++){
sum += arr[i];
}
return sum;
}
int main(){
int arr[] = {1,3,5,7,9};
int summary = sum(arr,5);
cout << summary << endl;
}

C++11给出的替代方案，实际上该方案仍然使用模板参数传递了数组的长度，所以该参数必须要传入。并且编译器请选择：Having g++ following the coming C++0x ISO C++ language standard [-std=c++0x]

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
int sum(array<int,SIZE> arr){
int sum = 0;
int length = arr.size();
for (int i = 0 ; i < length; i++){
sum += arr[i];
}
return sum;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};
int summary = sum(arr);
cout << summary << endl;
}

### 数组作为函数返回值

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
int* sum(array<int,SIZE> arr){
int length = arr.size();
int* returnArr = new int[length];
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return returnArr;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
int* summary = sum(arr);
for (int i = 0 ; i < arr.size(); i++){
cout << summary[i] << " ";
}
delete[] summary;
return 0;
}


#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
int* sum(array<int,SIZE> arr){
int length = arr.size();
static int returnArr[SIZE];
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return returnArr;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
int* summary = sum(arr);
for (unsigned int i = 0 ; i < arr.size(); i++){
cout << summary[i] << " ";
}
return 0;
}

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
int* sum(array<int,SIZE> arr){
int length = arr.size();
int returnArr[SIZE];
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return returnArr;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
int* summary = sum(arr);
for (unsigned int i = 0 ; i < arr.size(); i++){
cout << summary[i] << " ";
}
return 0;
}
#include <iostream>
using namespace std;
int* sum(int* arr,int length){
int returnArr[length];
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return returnArr;
}

int main(){
int arr[] = {1,3,5,7,9};
int length = sizeof(arr)/sizeof(arr[0]);
int* summary = sum(arr,length);
for (int i = 0 ; i < length; i++){
cout << summary[i] << " ";
}
}

#include <iostream>
using namespace std;
int* sum(int* arr,int length){
static int returnArr[length];
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return returnArr;
}

int main(){
int arr[] = {1,3,5,7,9};
int length = sizeof(arr)/sizeof(arr[0]);
int* summary = sum(arr,length);
for (int i = 0 ; i < length; i++){
cout << summary[i] << " ";
}
}

#include <iostream>
using namespace std;
int* sum(int* arr,int length){
int* returnArr = new int[length];
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return returnArr;
}

int main(){
int arr[] = {1,3,5,7,9};
int length = sizeof(arr)/sizeof(arr[0]);
int* summary = sum(arr,length);
for (int i = 0 ; i < length; i++){
cout << summary[i] << " ";
}
delete[] summary;
return 0;
}

### 其它可行方案

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
void sum(array<int,SIZE> arr,array<int,SIZE>& returnArr){
int length = arr.size();
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
array<int,5> returnArr;
sum(arr,returnArr);
for (unsigned int i = 0 ; i < returnArr.size(); i++){
cout << returnArr[i] << " ";
}
return 0;
}

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
void sum(array<int,SIZE> arr,array<int,SIZE> returnArr){
int length = arr.size();
for (int i = 0 ; i < length; i++){
returnArr[i] = arr[i];
}
return;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
array<int,5> returnArr;
sum(arr,returnArr);
for (unsigned int i = 0 ; i < returnArr.size(); i++){
cout << returnArr[i] << " ";
}
return 0;
}

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
void sum(array<int,SIZE> arr){
int length = arr.size();
for (int i = 0 ; i < length; i++){
arr[i] = 0;
}
return;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
sum(arr);
for (unsigned int i = 0 ; i < arr.size(); i++){
cout << arr[i] << " ";
}
return 0;
}
//输出结果为: 1 3 5 7 9

#include <iostream>
#include <array>
using namespace std;

template <size_t SIZE>
void sum(array<int,SIZE>& arr){
int length = arr.size();
for (int i = 0 ; i < length; i++){
arr[i] = 0;
}
return;
}

int main(){
array<int,5> arr {{1,3,5,7,9}};    // C++ 11 standard
sum(arr);
for (unsigned int i = 0 ; i < arr.size(); i++){
cout << arr[i] << " ";
}
return 0;
}
//输出结果： 0 0 0 0 0

### 总结

• 在std::array中，由于使用了template参数，使得在编译时获取到数组的长度，因此加static关键字即可返回正确的数组内容
• 在传统的语法中，只能在堆区分配空间才能解决数组作为参数返回问题，但是要注意释放指针空间，否则会造成悬挂指针。
• 如果长度不确定，可以考虑vector作为替代
• std::array采用按值传递方式，如果想要改变其值，需要采用引用方式传值