public member function
<stack>
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
none
Return 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// 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:
Complexity
Constant.
See also
- stack::pop
- Remove element (public member function)
- stack::push
- Add element (public member function)