cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : stack : top
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
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

// 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)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us