English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans cet article, vous apprendrez à transmettre des arrays à C ++Les fonctions que vous apprendrez à transmettre incluent les arrays unidimensionnels et multidimensionnels.
dans l'arraypeut être transmise comme paramètreLa fonction。Il est également possible de renvoyer un array à partir de la fonction. Considérez l'exemple suivant, transmettre un array unidimensionnel à la fonction:
C ++Le programme affiche les notes en transmettant un array unidimensionnel à la fonction5les notes des étudiants.
#include <iostream> using namespace std; void display(int marks[5]); int main() { int marks[5]] = {88, 76, 90, 61, 69}; display(marks); return 0; } void display(int m[5]) { cout << "Afficher les notes: " << endl; for (int i = 0; i < 5; ++i) { cout << "Student " << i + 1 <<": "<< m[i] << endl; } }
输出结果
Afficher les notes: Student 1: 88 Student 2: 76 Student 3: 90 Student 4: 61 Student 5: 69
Lorsque l'array est transmis comme paramètre à une fonction, seule le nom de l'array est utilisé comme paramètre.
display(marks);
Il faut aussi noter la différence entre transmettre un array en tant que paramètre et une variable.
void display(int m[5]);
Dans le code ci-dessus, le paramètre marks représente l'array marks[5] est l'adresse en mémoire du premier élément.
La forme des paramètres dans la déclaration de la fonction int m [5] est converti en int * m;, ce pointeur pointe vers l'adresse de la même mémoire que l'array marks.
C'est la raison pour laquelle, bien que la fonction soit utilisée avec des noms d'arrays différents dans les fonctions définies par l'utilisateur m[5]]进行操作,但是原始数组仍在marks进行操作。
C ++以这种方式处理将数组传递给函数以节省内存和时间。
Tableau multidimensionnel可以通过与一维数组相似的方式传递。考虑以下示例,将二维数组传递给函数:
C ++程序通过将二维数组的元素传递给函数来显示它。
#include <iostream> using namespace std; void display(int n[3]]2]); int main() { int num[3]]2]] = { {3, 4}, {9, 5}, {7, 1} }; display(num); return 0; } void display(int n[3]]2]) { cout << "显示值: " << endl; for(int i = 0; i < 3; ++i) { for(int j = 0; j < 2; ++j) { cout << n[i][j] << " "; } } }
输出结果
显示值: 3 4 9 5 7 1
在上面的程序中,多维数组num被传递给函数display()。
在display()函数内部,使用嵌套的for循环遍历数组n(num)。
该程序使用2个for循环遍历二维数组中的元素。如果是一个三维数组,那么应该使用3 for循环。
最后,所有元素都被打印到屏幕上。
注意: 维度大于2的多维数组可以以类似于二维数组的方式传递。
C++ 不允许返回一个完整的数组作为函数的参数。但是,您可以通过指定不带索引的数组名来返回一个指向数组的指针。
如果您想要从函数返回一个一维数组,您必须声明一个返回指针的函数,如下:
int * myFunction() { . . . }
另外,C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量。
现在,让我们来看下面的函数,它会生成 10 个随机数,并使用数组来返回它们,具体如下:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; // 要生成和返回随机数的函数 int * getRandom() { static int r[8]; // 设置种子 srand((unsigned)time(NULL)); for (int i = 0; i < 8; ++i) { r[i] = rand(); cout << r[i] << endl; } return r; } // La fonction principale pour appeler la fonction définie précédemment int main () { // Un pointeur vers un entier int *p; p = getRandom(); for ( int i = 0; i < 8; i++ ) { cout << "*(p + " << i << ) : "; cout << *(p + i) << endl; } return 0; }
Lorsque le code suivant est compilé et exécuté, il produira le résultat suivant :
30737 23110 21765 14820 8295 12330 28395 191 *(p + 0) : 30737 *(p + 1) : 23110 *(p + 2) : 21765 *(p + 3) : 14820 *(p + 4) : 8295 *(p + 5) : 12330 *(p + 6) : 28395 *(p + 7) : 191