Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
stack
comparison operators
stack::stack
member functions:
stack::empty
stack::pop
stack::push
stack::size
stack::top


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

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:
mystack.top() is now 15

Complexity

Constant.

See also