vector::front e vector::back

Viene illustrato come utilizzare vettore:: parte precedente e vettore:: indietro Funzioni della libreria di modelli standard (STL) in Visual C++.

template<class _TYPE, class _A>
   reference vector::front( );
template<class _TYPE, class _A>
   reference vector::back( );

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

Nell'esempio viene dichiarato un vettore vuoto di Integer con i membri [100, 200, 300, 400].Verrà visualizzato il primo elemento del vettore tramite vector::front per ottenerla.Visualizzare l'ultimo elemento del vettore tramite vector::back per ottenerlo.Visualizza il numero degli elementi del vettore tramite vector::size.L'esempio cancella l'ultimo elemento del vettore tramite vector::end - 1 e viene visualizzato il nuovo l'ultimo elemento tramite vector::back.Cancella il primo elemento del vettore tramite vector::beginquindi viene visualizzato il nuovo primo elemento tramite vector::front.Infine, nell'esempio il numero di elementi che rimangono nel vettore tramite vector::size.

Esempio

// frontback.cpp
// compile with: /EHsc
//
// Illustrates the vector::front and vector::back methods.
//
// Functions:
//
// vector::front - Returns reference to first element of vector.
//
// vector::back - Returns reference to last element of vector.
//
// vector::push_back - Appends (inserts) an element to the end of a
//                        vector, allocating memory for it if necessary.
//
// vector::size - Returns number of elements in 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::erase - Deletes elements from a vector (single & range).
//
//////////////////////////////////////////////////////////////////////

// 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 issued.
#pragma warning(disable:4786)

#include <iostream>
#include <vector>

using namespace std ;

typedef vector<int> INTVECTOR;

const int ARRAY_SIZE = 4;

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

    // Intialize the array to contain the members [100, 200, 300, 400]
    for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
        theVector.push_back((cEachItem + 1) * 100);

    cout << "First element: " << theVector.front() << endl;
    cout << "Last element: " << theVector.back() << endl;
    cout << "Elements in vector: " << theVector.size() << endl;

    // Delete the last element of the vector. Remember that the vector
    // is 0-based, so theVector.end() actually points 1 element beyond
    // the end.
    theVector.erase(theVector.end() - 1);

    cout << endl << "After erasing last element, new last element is: "
         << theVector.back() << endl;

    // Delete the first element of the vector.
    theVector.erase(theVector.begin());

    cout << "After erasing first element, new first element is: "
         << theVector.front() << endl;

    cout << "Elements in vector: " << theVector.size() << endl;
}

Output

First element: 100
Last element: 400
Elements in vector: 4

After erasing last element, new last element is: 300
After erasing first element, new first element is: 200
Elements in vector: 2

Requisiti

intestazione: <vettore>

Vedere anche

Concetti

Esempi di una libreria di modelli standard