欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ 讓函數(shù)返回數(shù)組的方法

 更新時間:2020年07月17日 11:21:49   作者:菜鳥教程  
這篇文章主要介紹了C++ 讓函數(shù)返回數(shù)組的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

這個問題屬于非常初級的問題,但是對于初學(xué)不知道的人可能會比較頭疼。C++ 中函數(shù)是不能直接返回一個數(shù)組的,但是數(shù)組其實(shí)就是指針,所以可以讓函數(shù)返回指針來實(shí)現(xiàn)。比如一個矩陣相乘的函數(shù),很容易地我們就寫成:

#include <iostream>

using namespace std;

float* MultMatrix(float A[4], float B[4])
{
  float M[4];
  M[0] = A[0]*B[0] + A[1]*B[2];
  M[1] = A[0]*B[1] + A[1]*B[3];
  M[2] = A[2]*B[0] + A[3]*B[2];
  M[3] = A[2]*B[1] + A[3]*B[3];

  return M;
}

int main()
{
  float A[4] = { 1.75, 0.66, 0, 1.75 };
  float B[4] = {1, 1, 0, 0};
  float *M = MultMatrix(A, B);
  cout << M[0] << " " << M[1] << endl;
  cout << M[2] << " " << M[3] << endl;

  return 0;
}

但是運(yùn)行后發(fā)現(xiàn)結(jié)果是:

1.75 1.75
6.51468e-039 3.76489e-039

本不是想要的結(jié)果。于是我們在函數(shù)中也加上顯示代碼,看看是不是計算的問題,得到結(jié)果:

1.75 1.75
0 0
1.75 1.75
1.96875 1.75

發(fā)現(xiàn)計算的結(jié)果是正確的,但返回后就變了,而且跟上次的結(jié)果不一樣。這是為什么呢?

因?yàn)樵诤瘮?shù)中定義的數(shù)組M在函數(shù)執(zhí)行完后已經(jīng)被系統(tǒng)釋放掉了,所以在調(diào)用函數(shù)中得到的結(jié)果當(dāng)然不是計算后的結(jié)果。有一個解決辦法就是動態(tài)分配內(nèi)存,在函數(shù)中 new 一個數(shù)組,這樣就不會被釋放掉了。

于是就應(yīng)該將:

float M[4];

改為:

float *M = new float[4];

修改運(yùn)行后得到結(jié)果:

1.75 1.75
0 0
1.75 1.75
0 0

正確。但是我們這樣并沒有將自己申請的空間釋放掉,如果我們在函數(shù)內(nèi)釋放的話結(jié)果就會跟開始時的一樣了。

看看我們的調(diào)用代碼:

float *M = MultMatrix(A, B);

這樣其實(shí)是將M指針指向了函數(shù)中M數(shù)組的首地址,我們可以將M指針釋放,效果和釋放申請的M數(shù)組是一樣的,因?yàn)樗鼈冎赶虻氖峭黄瑑?nèi)存空間。于是代碼就修改為:

#include <iostream>

using namespace std;

float* MultMatrix(float A[4], float B[4])
{
  float *M = new float[4];
  M[0] = A[0]*B[0] + A[1]*B[2];
  M[1] = A[0]*B[1] + A[1]*B[3];
  M[2] = A[2]*B[0] + A[3]*B[2];
  M[3] = A[2]*B[1] + A[3]*B[3];
  cout << M[0] << " " << M[1] << endl;
  cout << M[2] << " " << M[3] << endl;

  return M;
}

int main()
{
  float A[4] = { 1.75, 0.66, 0, 1.75 };
  float B[4] = {1, 1, 0, 0};
  float *M = MultMatrix(A, B);
  cout << M[0] << " " << M[1] << endl;
  cout << M[2] << " " << M[3] << endl;
  delete[] M;

  return 0;
}

運(yùn)行結(jié)果:

1.75 1.75
0 0
1.75 1.75
0 0

沒有問題,new的空間也delete掉了。

鑒于下面大牛們的建議,我將程序修改如下,大家看可否:

#include <iostream>

using namespace std;

void MultMatrix(float M[4], float A[4], float B[4])
{
  M[0] = A[0]*B[0] + A[1]*B[2];
  M[1] = A[0]*B[1] + A[1]*B[3];
  M[2] = A[2]*B[0] + A[3]*B[2];
  M[3] = A[2]*B[1] + A[3]*B[3];

  cout << M[0] << " " << M[1] << endl;
  cout << M[2] << " " << M[3] << endl;
}

int main()
{
  float A[4] = { 1.75, 0.66, 0, 1.75 };
  float B[4] = {1, 1, 0, 0};

  float *M = new float[4];
  MultMatrix(M, A, B);

  cout << M[0] << " " << M[1] << endl;
  cout << M[2] << " " << M[3] << endl;
  delete[] M;

  return 0;
}

點(diǎn)評內(nèi)容:

首先,數(shù)組的 delete 是 delete[]。

其次,C++ 里面手動內(nèi)存分配的一個重要原則是誰分配誰釋放。

所以,不應(yīng)該在MultMatrix里new數(shù)組,而應(yīng)該在外面new好了之后傳進(jìn)去修改。

要想返回一個數(shù)組,使用智能指針之類的東西才是正途。

以上就是C++ 讓函數(shù)返回數(shù)組的方法的詳細(xì)內(nèi)容,更多關(guān)于C++ 讓函數(shù)返回數(shù)組的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論