
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
deque::empty() and deque::size() in C++ STL
In this article we will be discussing the working, syntax and examples of deque::empty() and deque::size() functions in C++ STL.
What is Deque?
Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the insertion and deletion of data is possible at both the ends.
What is deque::empty()?
deque::empty() is an inbuilt function in C++ STL which is declared in <deque> header file. deque::empty() checks whether the given deque container is empty(size is 0) or not, and returns true value if the container associated with the function is empty and false if the container is not empty.
Syntax
mydeque.empty();
Parameter
This function accepts no parameter
Return value
This function returns true if the given deque container is empty or the size of the deque container is 0, else the function returns false if there are some elements in the deque associated with the function or the size of the container is not zero.
Example
Input: deque<int> mydeque = {10, 20 30, 40}; mydeque.empty(); Output: False Input: deque<int> mydeque; mydeque.empty(); Output: True
Example
#include <deque> #include <iostream> using namespace std; int main(){ int product = 1; deque<int> Deque; //inserting elements to a deque using push_back() function Deque.push_back(10); Deque.push_back(20); Deque.push_back(30); Deque.push_back(40); Deque.push_back(50); //traversing deque while it willn’t gets empty using empty() function while(!Deque.empty()){ product = product * Deque.front(); Deque.pop_front(); } //check if deque is empty or not if(Deque.empty()){ cout<<"Deque is empty and hence product is : "<<product; } else { cout<<"Deque is not empty "; } return 0; }
Output
If we run the above code it will generate the following output −
Deque is empty and hence product is : 12000
What is deque::size()?
deque::size() is an inbuilt function in C++ STL which is declared in header file. deque::size() returns the size of the deque container associated with the function. If the container has no elements then the function returns 0.
Syntax
mydeque.size();
Parameter
This function accepts no parameter
Return value
This function returns an unsigned integer value i.e., the number of elements present in the container.
Example
Input: deque<int> mydeque = {10, 20 30, 40}; mydeque.size(); Output: 4 Input: deque<int> mydeque; mydeque.size(); Output: 0
Example
#include <deque> #include <iostream> using namespace std; int main(){ int product = 0; deque<int> Deque; //inserting elements to a deque Deque.push_back(10); Deque.push_back(20); Deque.push_back(30); Deque.push_back(40); Deque.push_back(50); //checking the size of a deque cout<<"size of deque is : "<<Deque.size(); return 0; }
Output
If we run the above code it will generate the following output −
Size of deque is: 5
- Related Articles
- deque::at() and deque::swap() in C++ STL
- deque::begin() and deque::end in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- deque front( ) and deque back( ) in C++ in STL
- Deque emplace_front( ) and deque emplace_back( ) in C++ in STL
- deque::at() and deque::swap() in C++ programming STL
- deque::push_front() in C++ STL
- Deque shrink_to_fit in C++ STL
- deque push_back( ) in C++ in STL
- deque assign() function in C++ STL
- C++ Program to Implement Deque in STL
- DEQUE CRBEGIN() in C++
- DEQUE CBEGIN() in C++
- A Deque Class in C#
- Deque in Java
