.empty()
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 integersstd::deque<int> numbers;// Add elements to the dequenumbers.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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours