Namespaces
Variants

std::stack<T,Container>:: top

From cppreference.net
reference top ( ) ;
(1)
const_reference top ( ) const ;
(2)

Retourne une référence à l'élément supérieur de la pile. Il s'agit de l'élément le plus récemment ajouté. Cet élément sera supprimé lors d'un appel à pop() . Équivalent à : c . back ( ) .

Table des matières

Paramètres

(aucun)

Valeur de retour

Référence au dernier élément.

Complexité

Constante.

Exemple

#include <iostream>
#include <stack>
void reportStackSize(const std::stack<int>& s)
{
    std::cout << s.size() << " elements on stack\n";
}
void reportStackTop(const std::stack<int>& s)
{
    // Leaves element on stack
    std::cout << "Top element: " << s.top() << '\n';
}
int main()
{
    std::stack<int> s;
    s.push(2);
    s.push(6);
    s.push(51);
    reportStackSize(s);
    reportStackTop(s);
    reportStackSize(s);
    s.pop();
    reportStackSize(s);
    reportStackTop(s);
}

Sortie :

3 elements on stack
Top element: 51
3 elements on stack
2 elements on stack
Top element: 6

Voir aussi

insère un élément au sommet
(fonction membre publique)
supprime l'élément du sommet
(fonction membre publique)