Utiliser les fonctions STL vector ::erase, vector ::empty et vector ::p ush_back dans Visual C++

Cet article explique comment utiliser la vector::erase fonction, la vector::empty fonction et les vector::push_back fonctions STL (Standard Template Library) dans Visual C++. Les informations s’appliquent uniquement au code Visual C++ non managé.

Version d’origine du produit : Visual C++
Numéro de la base de connaissances d’origine : 158612

En-tête obligatoire

<vector>

Prototypes

template<class _TYPE, class _A>
void vector::push_back(const _TYPE& X);

template<class _TYPE, class _A>
iterator vector::erase(iterator Iterator);

template<class _TYPE, class _A>
iterator vector::erase(iterator First, iterator Last);

template<class _TYPE, class _A>
bool vector::empty() const;

Remarque

Les noms de classe/paramètre dans le prototype peuvent ne pas correspondre à la version dans le fichier d’en-tête. Certains ont été modifiés pour améliorer la lisibilité.

Description

L’exemple déclare un vecteur vide d’entiers. Il ajoute 10 entiers au vecteur, puis affiche le contenu du vecteur. Il supprime le sixième élément à l’aide de l’effacement, puis affiche à nouveau le contenu du vecteur. Il supprime le reste des éléments à l’aide d’une autre forme d’effacement, puis affiche à nouveau le vecteur (maintenant vide). La ShowVector routine utilise la fonction vide pour déterminer s’il faut générer le contenu du vecteur.

Exemple de code

Dans Visual C++ .NET et dans Visual C++, /EHsc est défini par défaut et équivaut à /GX.

Remarque

Dans Visual C++, vous devez remplacer le code par const ARRAY_SIZE = 10;const int ARRAY_SIZE = 10; si vous souhaitez exécuter l’exemple de code.

//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// Empty.cpp -- Illustrates the vector::empty and vector::erase
// functions.
// Also demonstrates the vector::push_back function.
// Functions:
// vector::empty - Returns true if vector has no elements.
// vector::erase - Deletes elements from a vector (single & range).
// vector::begin - Returns an iterator to start traversal of the
// vector.
// vector::end - Returns an iterator for the last element of the
// vector.
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
// vector::iterator - Traverses the vector.
// of Microsoft Corporation
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
#pragma warning(disable:4786)

#include <iostream>
#include <vector>

#if _MSC_VER > 1020 // if VC++ version is > 4.2
    using namespace std; // std c++ libs implemented in std
#endif

typedef vector<int, allocator<int> > INTVECTOR;

const ARRAY_SIZE = 10;

void ShowVector(INTVECTOR &theVector);

void main()
{
     // Dynamically allocated vector begins with 0 elements.
     INTVECTOR theVector;

     // Intialize the vector to contain the numbers 0-9.
     for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
         theVector.push_back(cEachItem);

     // Output the contents of the dynamic vector of integers.
     ShowVector(theVector);

     // Using void iterator erase(iterator Iterator) to
     // delete the 6th element (Index starts with 0).
     theVector.erase(theVector.begin() + 5);

     // Output the contents of the dynamic vector of integers.
     ShowVector(theVector);

     // Using iterator erase(iterator First, iterator Last) to
     // delete a range of elements all at once.
     theVector.erase(theVector.begin(), theVector.end());

     // Show what's left (actually, nothing).
     ShowVector(theVector);
}

// Output the contents of the dynamic vector or display a
// message if the vector is empty.
void ShowVector(INTVECTOR &theVector)
{
     // First see if there's anything in the vector. Quit if so.
     if (theVector.empty())
     {
         cout << endl << "theVector is empty." << endl;
         return;
     }

     // Iterator is used to loop through the vector.
     INTVECTOR::iterator theIterator;

     // Output contents of theVector.
     cout << endl << "theVector [ " ;
     for (theIterator = theVector.begin(); theIterator != theVector.end();
     theIterator++)
     {
         cout << *theIterator;
         if (theIterator != theVector.end()-1) cout << ", ";
         // cosmetics for the output
     }
     cout << " ]" << endl ;
}

Sortie du programme :

theVector [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
theVector [ 0, 1, 2, 3, 4, 6, 7, 8, 9 ]
theVector is empty.

References

vector ::empty, vector ::erase et vector ::p ush_back