-->

C++ - C++ Deque Part 5: Checking Size and Clearing a Deque

The following functions help manage the deque:

size(): Returns the number of elements.

clear(): Removes all elements.

empty(): Checks if the deque is empty.

Example 9: Checking the Size of a Deque

#include <iostream>

#include <deque>

int main() {

    std::deque<int> dq = {1, 2, 3, 4, 5};

    std::cout << "Size of deque: " << dq.size() << std::endl;

    return 0;

}

Output:

Size of deque: 5

Example 10: Clearing the Deque

#include <iostream>

#include <deque>

int main() {

    std::deque<int> dq = {1, 2, 3, 4, 5};

    std::cout << "Size before clear: " << dq.size() << std::endl;

    dq.clear();  // Removes all elements

    std::cout << "Size after clear: " << dq.size() << std::endl;

    return 0;

}

Output:

Size before clear: 5

Size after clear: 0