public member function
<stack>

std::stack::size

size_type size() const;
Return size
Returns the number of elements in the stack.

This member function effectively calls member size of the underlying container object.

Parameters

none

Return Value

The number of elements in the underlying container.

Member type size_type is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// stack::size
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<5; i++) myints.push(i);
  std::cout << "1. size: " << myints.size() << '\n';

  myints.pop();
  std::cout << "2. size: " << myints.size() << '\n';

  return 0;
}


Output:
0. size: 0
1. size: 5
2. size: 4

Complexity

Constant (calling size on the underlying container).

Data races

The container is accessed.

Exception safety

Provides the same level of guarantees as the operation performed on the container (no-throw guarantee for standard container types).

See also