C++ Deque Clear Function

In the C++ programming language, the deque clear function is a predefined member function of the deque, i.e., double-ended queue. It can help to remove all the elements from the deque. Once we call the clear function, the deque becomes empty and size has the value zero, and its capacity may remain unchanged. It is helpful to efficiently reset the queue without removing the complete object, and thus it can be reused.

In C++ , the function certainly destroys all elements properly and frees the memory occupied by them, but the deque may have new elements placed in it without having to reallocate memory at once. It is clearly used whenever we need to reuse the same deque in a program to keep fresh data in it without constructing a new one.

Syntax:

It has the following syntax:

Example

deque_name.clear();

In this syntax,

Parameters:

  • The clear function does not take any parameters.
  • It simply removes all the elements present in the deque.
  • Return type:

  • It does not return any value (return type void).
  • After execution, the deque becomes empty. (size becomes 0).
  • C++ Simple deque clear function Example

Let us take an example to illustrate the deque clear function in C++.

Example

Example

#include <iostream>

#include <deque>

using namespace std;   //using standard namespace

int main() {   //main function

    deque<int> numbers = {10, 20, 30, 40, 50};

    cout << "Deque before clear(): ";

    for (int n : numbers)

        cout << n << " ";

    numbers.clear();  // Remove all elements

    cout << "\nDeque after clear(): ";

    for (int n : numbers)

        cout << n << " ";

    cout << "\nSize of deque: " << numbers.size();

    return 0;

}

Output:

Output

Deque before clear(): 10 20 30 40 50 

Deque after clear(): 

Size of deque: 0

Explanation:

In this example, we have created the deque with five integer values. After that, the elements in the deque are removed using the clear function. When we execute the clear function, the deque becomes empty, and the value of size returns 0 because all the elements have been removed successfully.

C++ deque clear Function Example to demonstrate the Reuse of the Container

Let us take an example to illustrate the deque clear function to demonstrate the reuse of the container in C++.

Example

Example

#include <iostream>

#include <deque>

using namespace std;   //using standard namespace

int main() {    //main function

    deque<int> values;

    // Inserting elements into the deque

    for (int i = 1; i <= 5; i++) {

        values.push_back(i * 10);

    }

    cout << "Deque before clear(): ";

    for (int val : values)

        cout << val << " ";

    // Clearing all elements from the deque

    values.clear();

    // Inserting new elements after clear

    values.push_back(100);

    values.push_back(200);

    cout << "\nDeque after clear() and inserting new elements: ";

    for (int val : values)

        cout << val << " ";

    cout << "\nCurrent size of deque: " << values.size();

    return 0;

}

Output:

Output

Deque before clear(): 10 20 30 40 50 

Deque after clear() and inserting new elements: 100 200 

Current size of deque: 2

Explanation:

In this example, the push_back function is used to insert values in the deque. Thereafter, the clear function wipes all the elements, leaving the deque empty. The deque can still be utilized without any problems because fresh elements are introduced after clearing the elements. The output attests that the deque now contains solely the recently added elements.

Features of the deque clear function in C++

Several features of the deque clear function in C++ are as follows:

  • In the C++ programming language, the deque clear function is commonly utilized to remove all the elements from the deque until it becomes zero or empty.
  • This function is very helpful if we want to reset or reinitialize the deque without reallocating the memory in the deque.
  • This function doesn't return any value in C++. The return type of the deque clear function is void.
  • It takes an average time complexity (O(n)) because it needs to delete each and every element separately from the deque.
  • It doesn't destroy the deque object itself, which enables it to be reused after clearing the element.
  • Conclusion

In conclusion, the C++ deque clear function is a member function of the deque container that is a part of STL. It gives a simple way to clear all of the elements of a deque container. This function resets the deque size to zero, which allows us to perform new insertion operation. However, the capacity limit of the deque may remain unchanged. It gives a simple method to reuse the current deque without making a new deque. It is very helpful in those cases where we need to refresh or reset the data frequently.

C++ deque clear Function FAQs

1) What is the purpose of the deque::clear function in C++?

The C++ deque clear method is a member function of the deque container, which is a part of the STL. It is used to delete all the elements in the deque until the deque becomes empty or zero.

2) Does the clear function free the memory used by the deque in C++?

In C++, the clear function is mainly utilized to delete all of the elements, but it doesn't require freeing the memory that has been allocated. However, the capacity limit of the deque may remain unchanged.

3) What is the time complexity for the clear function in C++?

In C++, the clear function has average time complexity O(n), where n represents the number of elements in the deque.

4) Can we reuse the deque after having called the clear function in C++?

Yes, we can reuse the deque function after calling the clear function in C++. The deque remains valid, and it can be utilized to insert a new element in the deque just as before.

5) What is the main difference between the clear function and the erase function in C++?

In C++, the main difference between the clear and the erase functions is that the clear function is commonly used to remove all the elements from the deque. In contrast, the erase function is commonly utilized to remove the element from any specific position.

Input Required

This code uses input(). Please provide values below: