Ciao, ragazzi! Quindi, come programmatori, spesso ci troviamo ad affrontare array di tutti i tipi di dati. Oggi parleremo di array di stringhe in C++ nell’articolo di oggi.
Modi per dichiarare un array di stringhe in C++

1. La parola chiave String per creare un array di stringhe in C++
C++ provides us with ‘string’ keyword to declare and manipulate data in a String array.
La parola chiave string
alloca memoria agli elementi dell’array in modo dinamico o a tempo di esecuzione di conseguenza. Quindi risparmia il mal di testa dell’allocazione statica della memoria degli elementi di dati.
Sintassi: Per dichiarare un array di stringhe usando la parola chiave ‘string’
string array-name[size];
Inoltre, possiamo inizializzare l’array di stringhe usando la seguente sintassi:
string array-name[size]={'val1','val2',.....,'valN'};
Esempio 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string fruits[5] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" };
cout<<"String array:\n";
for (int x = 0; x< 5; x++)
cout << fruits[x] << "\n";
}
Nell’esempio precedente, abbiamo inizializzato l’array di stringhe e abbiamo usato i cicli for di C++ per attraversare l’array e stampare gli elementi di dati presenti nell’array di stringhe.
Output:
String array:
Grapes
Apple
Pineapple
Banana
Jackfruit
Esempio 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string arr[5];
cout<<"Enter the elements:"<<endl;
for(int x = 0; x<5;x++)
{
cin>>arr[x];
}
cout<<"\nString array:\n";
for (int x = 0; x< 5; x++)
cout << arr[x] << "\n";
}
Come potete vedere tutti, nell’esempio precedente, abbiamo accettato gli elementi di dati dell’array di stringhe dalla console, cioè abbiamo preso in input dall’utente e abbiamo stampato gli elementi dell’array di stringhe.
Uscita:
Enter the elements:
Jim
Nick
Daisy
Joha
Sam
String array:
Jim
Nick
Daisy
Joha
Sam
2. Utilizzo del contenitore STL di C++ – Vector
C++ Standard Template Library provides us with containers to work with data and store it efficiently.
Vector, essendo uno dei tali contenitori, memorizza gli elementi dell’array in modo dinamico. Quindi, i vettori C++ possono essere utilizzati per creare un array di stringhe e manipolarlo facilmente.
Sintassi:
vector<string>array-name;
- Il metodo
vector.push_back(element)
viene utilizzato per aggiungere elementi all’array di stringhe vettoriali. - Il metodo
vector.size()
viene utilizzato per calcolare la lunghezza dell’array, cioè il conteggio degli elementi inseriti nell’array di stringhe.
Esempio:
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<string> arr;
arr.push_back("Ace");
arr.push_back("King");
arr.push_back("Queen");
int size = arr.size();
cout<<"Elements of the vector array:"<<endl;
for (int x= 0; x< size; x++)
cout << arr[x] << "\n";
}
Uscita:
Elements of the vector array:
Ace
King
Queen
3. Utilizzo di un array di caratteri 2D
A 2D array represents an array of string in C++. So, we can use a 2D char array to represent string type elements in an array.
L’array di caratteri crea e memorizza gli elementi in modo statico o a tempo di compilazione, cioè il numero e la dimensione degli elementi rimangono fissi/ costanti.
Sintassi:
char array-name[number-of-items][maximun_size-of-string];
Esempio:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char fruits[5][10] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" };
cout<<"Character array:\n";
for (int x = 0; x< 5; x++)
cout << fruits[x] << "\n";
}
Nello snippet di codice sopra, abbiamo creato un array di caratteri per memorizzare elementi di tipo stringa. Cioè, char array[5][10]. Qui 5 rappresenta il conteggio degli elementi di stringa e 10 indica la dimensione massima della stringa di input.
Output:
Character array:
Grapes
Apple
Pineapple
Banana
Jackfruit
C++ String Array as an Argument to a Function
A string array can also be passed to a function as an argument the same way as another non-string type array is passed to it.
Sintassi:
return-type function-name(string array-name[size])
{
// corpo della funzione
}
Esempio:
#include <iostream>
#include<string>
using namespace std;
void show(string arr[4]){
for(int x=0;x<4;x++)
{
cout<<arr[x]<<endl;
}
}
int main() {
string arr[4] = {"Jim", "Jeo", "Jio", "John"};
cout<<"Printing elements of the string array:"<<endl;
show(arr);
}
Output:
Printing elements of the string array:
Jim
Jeo
Jio
John
Conclusione
In questo articolo, abbiamo compreso i modi per creare array di stringhe e le tecniche per usarli in una funzione.
Riferimenti
Source:
https://www.digitalocean.com/community/tutorials/string-array-in-c-plus-plus