C++ Standard Library C++ STL Library

C++ <deque> - size() Function



The C++ deque::size function is used to find out the total number of elements in the deque. It returns the total number of elements held in the deque, and it is not necessarily equal to its storage capacity.

Syntax

size_type size() const;
size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the deque.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the deque::size function is used find out the total number of elements in a deque called MyDeque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque{10, 20, 30, 40, 50};

  cout<<"Deque size is: "<<MyDeque.size()<<"\n";

  cout<<"Three elements are added in the Deque.\n";
  MyDeque.push_back(60);
  MyDeque.push_back(70);
  MyDeque.push_back(80);

  cout<<"Now, Deque size is: "<<MyDeque.size()<<"\n";
  return 0;
}

The output of the above code will be:

Deque size is: 5
Three elements are added in the Deque.
Now, Deque size is: 8

❮ C++ <deque> Library

5