stack::top | public member function |
value_type& top ( ); const value_type& top ( ) const; |
Access next element
Returns a reference to the next element in the stack. Since stacks are last-in first-out containers this is also the last element pushed into the stack.
This member function effectively calls the member function back of the underlying container object.
Parameters
noneReturn value
A reference to the next element in the stack.Member type value_type is defined to the type of value contained by the underlying container, which shall be the same as the first template parameter (T).
Example
// stack::top #include <iostream> #include <stack> using namespace std; int main () { stack<int> mystack; mystack.push(10); mystack.push(20); mystack.top() -= 5; cout << "mystack.top() is now " << mystack.top() << endl; return 0; } |
Output:
mystack.top() is now 15 |
Complexity
Constant.See also
stack::pop | Remove element (public member function) |
stack::push | Add element (public member function) |