Bibliothèque de chaînes C ++ - shrink_to_fit

La description

Il demande à la chaîne de réduire sa capacité pour s'adapter à sa taille.

Déclaration

Voici la déclaration pour std :: string :: shrink_to_fit.

void shrink_to_fit();

C ++ 11

void shrink_to_fit();

Paramètres

aucun

Valeur de retour

aucun

Des exceptions

si une exception est levée, il n'y a aucun changement dans la chaîne.

Exemple

Dans l'exemple ci-dessous pour std :: string :: shrink_to_fit.

#include <iostream>
#include <string>

int main () {
   std::string str (500,'x');
   std::cout << "1. capacity of str: " << str.capacity() << '\n';

   str.resize(10);
   std::cout << "2. capacity of str: " << str.capacity() << '\n';

   str.shrink_to_fit();
   std::cout << "3. capacity of str: " << str.capacity() << '\n';

   return 0;
}

L'exemple de sortie devrait être comme ceci -

1. capacity of str: 500
2. capacity of str: 500
3. capacity of str: 10