Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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 below example, 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\n";

  cout<<"Three elements in the Deque is added.\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 in the Deque is added.
Now, Deque size is: 8

❮ C++ - Deque