.empty()

abereFejiro9953409650's avatar
Published Nov 8, 2024
Contribute to Docs

In C++, the .empty() method checks if a given deque container is empty (i.e., if its size is 0). The method returns true if it is empty and false otherwise.

Syntax

dequeName.empty();
  • dequeName: The variable name of the deque being checked for emptiness.

Example

The example below showcases the use of the .empty() method:

#include <iostream>
#include <deque>
int main() {
// Create a deque of integers
std::deque<int> numbers;
// Add elements to the deque
numbers.push_back(100);
numbers.push_back(150);
std::cout << "Deque is empty: ";
if (numbers.empty()) {
std::cout << "True";
}
else {
std::cout << "False";
}
std::cout << std::endl;
return 0;
}

The above code generates the following output:

Deque is empty: False

Codebyte Example

The following codebyte uses the .clear() method on a deque and checks its emptiness by outputting a boolean value using the .empty() method:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy