Compartilhar via


vector::push_back e vector::pop_back

Ilustra como usar o vector::push_back e vector::pop_back funções de biblioteca STL (Standard Template) no Visual C++.

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

Comentários

ObservaçãoObservação

Nomes de classe/parâmetro o protótipo não coincidem com a versão no arquivo de cabeçalho.Alguns foram modificados para melhorar a legibilidade.

O exemplo declara um vetor de inteiros de vazio.Ele adiciona três números inteiros para o vetor e, em seguida, exclui um.Finalmente, ele gera os demais elementos do vetor.

Exemplo

// Pushpop.cpp
// compile with: /EHsc
// Illustrates how to use the push and pop member
// functions of the vector container.
//
// Functions:
//
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
//
// vector::pop_back -  Erases the last element of the vector.
//
// vector::begin - Returns an iterator to start traversal of the vector.
//
// vector::end - Returns an iterator for the last element of the vector.
//
// vector::iterator - Traverses the vector.
//
//////////////////////////////////////////////////////////////////////

// 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>

using namespace std ;

typedef vector<int> INTVECTOR;

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

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

    // Add one element to the end of the vector, an int with the value 42.
    // Allocate memory if necessary.
    theVector.push_back(42) ;

    // Add two more elements to the end of the vector.
    // theVector will contain [ 42, 1, 109 ].
    theVector.push_back(1) ;
    theVector.push_back(109) ;

    // Erase last element in vector.
    theVector.pop_back();

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

Requisitos

Cabeçalho: <vector>

Consulte também

Referência

abs (<valarray>)

Conceitos

Exemplos de biblioteca de modelo padrão