std::stack<T,Container>:: top
From cppreference.net
|
reference top
(
)
;
|
(1) | |
|
const_reference top
(
)
const
;
|
(2) | |
Devuelve una referencia al elemento superior en la pila. Este es el elemento más recientemente insertado. Este elemento será eliminado al llamar a
pop()
. Equivalente a:
c
.
back
(
)
.
Contenidos |
Parámetros
(ninguno)
Valor de retorno
Referencia al último elemento.
Complejidad
Constante.
Ejemplo
Ejecutar este código
#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); }
Salida:
3 elements on stack Top element: 51 3 elements on stack 2 elements on stack Top element: 6
Véase también
|
inserta elemento en la parte superior
(función miembro pública) |
|
|
elimina el elemento superior
(función miembro pública) |