public member function
<stack>
std::stack::top
value_type& top();const value_type& top() const;
reference top();const_reference top() const;
Access next element
Since stacks are last-in first-out containers, the top element is the last element inserted into the stack.
This member function effectively calls member back of the underlying container object.
Parameters
noneReturn value
A reference to the top element in the stack.
Member type value_type is the type of the elements in the container (defined as an alias of the first class template parameter, T).
Member types reference and const_reference are aliases of the underlying container's types with the same name.
Example
|
|
Output:
Complexity
Constant (calling back on the underlying container).Data races
The container is accessed (neither the const nor the non-const versions modify the container).The reference returned can be used to access or modify the top element.
Exception safety
Provides the same level of guarantees as the operation performed on the container (no-throw guarantee for standard non-empty containers).See also
See also
- stack::pop
- Remove top element (public member function)
- stack::push
- Insert element (public member function)