Introduction
In this lab, we will learn how to initialize vectors in C++. A vector is a dynamically resizable array with the ability to resize itself depending on the number of elements to be inserted or deleted. Vectors are more advantageous than ordinary arrays, which are of fixed size and are static in nature.
Créer un nouveau fichier C++
Dans votre terminal, accédez au répertoire du projet en utilisant la commande cd. Créez un nouveau fichier C++ nommé main.cpp en utilisant la commande suivante :
touch ~/project/main.cpp
Ouvrez le fichier avec votre éditeur de code préféré.
Initialiser un vecteur comme un tableau
Créez et initialisez un vecteur comme un tableau en utilisant le code suivant :
// Initializing a vector like an array
vector<int> v{1, 2, 3, 4};
// Print the vector
cout << "The elements of the first vector are: ";
for (int i : v)
{
cout << i << " ";
}
Cela créera un vecteur v et l'initialisera avec quatre éléments. Nous pouvons ensuite afficher les éléments de ce vecteur en utilisant la boucle for.
Initialisation d'un vecteur à partir d'un tableau
Créez et initialisez un vecteur à partir d'un tableau en utilisant le code suivant :
// Initializing a vector from an array
int a[] = {11, 22, 33};
int n = sizeof(a) / sizeof(a[0]); // number of elements in the array
vector<int> v1(a, a + n); // create and initialize the vector from the array
// Print the vector
cout << "The elements of the second vector are: ";
for (int i : v1)
{
cout << i << " ";
}
Cela créera un tableau a et l'initialisera avec trois éléments. Nous pouvons ensuite calculer le nombre d'éléments dans ce tableau, créer un vecteur v1 et l'initialiser avec les éléments du tableau. Nous pouvons ensuite afficher les éléments de ce vecteur en utilisant la boucle for.
Initialisation d'un vecteur à partir d'un autre vecteur
Créez et initialisez un vecteur à partir d'un autre vecteur en utilisant le code suivant :
// Initializing a vector from another vector
vector<int> a1 = {10, 22, 33};
vector<int> b(a1.begin(), a1.end()); // create and initialize the second vector with the elements of the first vector
// Print the vector
cout << "The elements of the third vector are: ";
for (int i : b)
{
cout << i << " ";
}
Cela créera un vecteur a1 et l'initialisera avec trois éléments. Nous pouvons ensuite créer un autre vecteur b et l'initialisera avec les éléments du vecteur a1. Nous pouvons ensuite afficher les éléments du vecteur b en utilisant la boucle for.
Compiler et exécuter le code
Enregistrez les modifications apportées au fichier main.cpp. Compilez le code en utilisant la commande suivante :
g++ main.cpp -o main
Exécutez le fichier exécutable en utilisant la commande suivante :
./main
Voir la sortie
Affichez la sortie dans le terminal. Vous devriez voir la sortie suivante :
The elements of the first vector are: 1 2 3 4
The elements of the second vector are: 11 22 33
The elements of the third vector are: 10 22 33
Résumé
Dans ce laboratoire (lab), nous avons appris à initialiser des vecteurs en C++ en utilisant différentes méthodes. Nous avons utilisé un tableau pour initialiser un vecteur, initialisé un vecteur avec les éléments d'un autre vecteur et initialisé un vecteur comme un tableau. Connaître les différentes façons d'initialiser des vecteurs en C++ peut nous aider à écrire un code plus efficace et plus lisible.



