Bibliothèque de vecteurs C ++ - fonction shrink_to_fit ()

La description

La fonction C ++ std::vector::shrink_to_fit() demande au conteneur de réduire sa capacité pour s'adapter à sa taille.

Déclaration

Voici la déclaration de la fonction std :: vector :: shrink_to_fit () sous forme d'en-tête std :: vector.

C ++ 98

void shrink_to_fit();

Paramètres

Aucun

Valeur de retour

Aucun

Exemple

L'exemple suivant montre l'utilisation de la fonction std :: vector :: shrink_to_fit ().

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v(128);

   cout << "Initial capacity = " << v.capacity() << endl;

   v.resize(25);
   cout << "Capacity after resize = " << v.capacity() << endl;

   v.shrink_to_fit();
   cout << "Capacity after shrink_to_fit = " << v.capacity() << endl;

   return 0;
}

Compilons et exécutons le programme ci-dessus, cela produira le résultat suivant -

Initial capacity = 128
Capacity after resize = 128
Capacity after shrink_to_fit = 25