English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In questo articolo, imparerai a utilizzare gli array. Imparerai a dichiarare, inizializzare e accedere agli elementi degli array nel programming C++.
Uno dei problemi più comuni nel programming è gestire una grande quantità di dati dello stesso tipo.
Ad esempio, in questo caso, stai conducendo un sondaggio su 100 persone e devi memorizzare le loro età. Per risolvere questo problema in C++, puoi creare un array di 100 elementi interi.
Un array è un insieme di dati che contiene una quantità fissa di valori dello stesso tipo. Ad esempio:
int age[100];
In questo esempio, l'array age può contenere al massimo 100 elementi di tipo intero.
Dopo aver dichiarato l'array, la dimensione e il tipo dell'array non possono essere modificati.
dataType arrayName[arraySize];
Ad esempio,
float mark[5];
In questo esempio, dichiariamo un array di tipo float e dimensione 5 chiamato mark. Questo significa che l'array mark può contenere 5 valori di tipo float.
Puoi accedere agli elementi dell'array utilizzando l'indice.
Supponiamo che tu abbia dichiarato un array mark come descritto sopra. Il primo elemento è mark[0], il secondo elemento è mark[1], e così via.
L'indice del primo elemento dell'array è 0, non 1. In questo esempio, mark[0] è il primo elemento.
Se la dimensione dell'array è n, per accedere all'ultimo elemento si utilizzerà l'indice (n-1). In questo esempio, mark[4] è l'ultimo elemento.
Supponiamo che l'indirizzo di partenza di mark[0] sia 2120d. Poi, l'indirizzo del successivo a[1] sarà 2124d, l'indirizzo di a[2] sarà 2128d, e così via. Questo perché la dimensione di float è di 4 byte.
Nel processo di dichiarazione è possibile inizializzare l'array. Ad esempio,
int mark[5] = {19, 10, 8, 17, 9};
Another way to initialize an array during declaration:
int mark[] = {19, 10, 8, 17, 9}
Here,
mark[0] = 19 mark[1] = 10 mark[2] = 8 mark[3] = 17 mark[4] = 9
int mark[5] = {19, 10, 8, 17, 9} // Change the fourth element to 9 mark[3] = 9; // Change the fourth element to 9 cin >> mark[2]; // Get input from the user and insert the third element cin >> mark[i]; // Accept user input and insert the (i + 1)th element // Print the first element of the array // Print the i-th element of the array cout >> mark[i-1];
C++ program used to store and calculate the sum of 5 numbers entered by the user using an array.
#include <iostream> using namespace std; int main() { int numbers[5], sum = 0; cout << "Enter 5 numbers: "; // Store the 5 numbers entered by the user in the array // Find the sum of the entered numbers for (int i = 0; i < 5; ++i) { cin >> numbers[i]; sum += numbers[i]; } cout << "Total = " << sum << endl; return 0; }
Output result
Enter 5 numbers: 3 4 5 4 2 Total = 18
Suppose you declare an array consisting of 10 elements. For example
int testArray[10];
You can use access to the array members from testArray[0] to testArray[9].
If you try to access array elements outside the array boundaries, such as testArray[14], the compiler may not display any errors. However, this may lead to unexpected output (undefined behavior).
Before proceeding, please review the following C++ array article:
In C++, array is very important, and we need to know more about the details of arrays. Below are some important concepts related to arrays that C++ programmers must be clear about:
Concetto | Descrizione |
---|---|
Array multidimensionali | C++ supporta gli array multidimensionali. La forma più semplice degli array multidimensionali è l'array bidimensionale. |
Puntatori a array | Puoi generare un puntatore al primo elemento dell'array specificando il nome dell'array senza indici. |
Passaggio di array a funzioni | Puoi passare un puntatore all'array senza specificare l'indice alla funzione. |
Restituzione di array da funzioni | C++ consente di restituire array da funzioni. |