Namespaces
Variants

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

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

스택의 최상위 요소에 대한 참조를 반환합니다. 이는 가장 최근에 푸시된 요소입니다. 이 요소는 pop() 호출 시 제거됩니다. 다음 코드와 동일합니다: c . back ( ) .

목차

매개변수

(없음)

반환값

마지막 요소에 대한 참조.

복잡도

상수.

예제

#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)
{
    // 스택에 요소를 남겨둠
    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);
}

출력:

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

참고 항목

맨 위에 요소를 삽입합니다
(public member function)
맨 위 요소를 제거합니다
(public member function)