C++ .empty()
Published Nov 8, 2024
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:
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours