C++ Deque Clear Function - C++ Programming Tutorial
C++ Course / STL Queue & Stack / C++ Deque Clear Function

C++ Deque Clear Function

BLUF: Mastering C++ Deque Clear Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Deque Clear Function

C++ is renowned for its efficiency. Learn how C++ Deque Clear Function enables low-level control and high-performance computing in the tutorial below.

In C++, the clear method in deque is a built-in function that can be used to eliminate all elements from the deque data structure. Invoking clear results in an empty deque with a size of zero, while its capacity may stay the same. This functionality allows for a quick and efficient way to reset the deque without needing to delete the entire object, making it ready for reuse.

In C++, the function effectively cleans up all elements and releases the memory they were using. However, the deque can accommodate additional elements without an immediate need to reallocate memory. This feature is particularly handy when there is a requirement to update the existing deque with new data without the overhead of creating 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's consider a scenario to demonstrate the deque clear method 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 instance, a deque was initialized with five integer values. Subsequently, the elements within the deque were eliminated utilizing the clear method. Upon executing the clear function, the deque transitions to an empty state, and the size method confirms this by returning a value of 0, indicating the successful removal of all elements.

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

Let's consider an instance to showcase the deque clear method in C++ and exhibit how it allows for the reutilization of the container.

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 instance, the push_back method is employed to add values to the deque. Subsequently, the clear function erases all elements, resulting in an empty deque. Despite this, the deque remains functional as new elements are added post-clearing. The result confirms that the deque now exclusively holds the newly inserted 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 summary, the clear method in C++ for deques is a member function within the deque container, a component of the Standard Template Library (STL). It offers a straightforward approach to remove all elements from a deque container. This method effectively sets the deque's size to zero, enabling the addition of new elements. Notably, the capacity limit of the deque may persist despite the clearing process. This function provides a convenient means to repurpose the existing deque rather than creating a new one, proving particularly beneficial when frequent data refreshing or resetting is required.

C++ deque clear Function FAQs

The deque::clear function in C++ serves the purpose of removing all elements from the deque, resulting in an empty container.

The clear function in C++ deque is a method belonging to the deque container within the STL. Its purpose is to remove all elements from the deque until it is empty or contains zero elements.

No, the clear function in C++ does not free the memory used by the deque.

In C++, the clear method is primarily used to remove all elements without releasing the allocated memory. Nonetheless, the deque's capacity limit may persist unchanged.

The time complexity for the clear function in C++ is linear, or O(n), where n represents the number of elements in the container that is being cleared.

In C++, the clear method has an average time complexity of O(n), with n denoting the total number of elements in the deque.

4) Is it possible to utilize the deque again after invoking the clear method in C++?

Yes, the deque function in C++ can be reused following the clear function call. The deque object retains its validity, allowing for the insertion of new elements into the deque as usual.

The primary distinction between the clear function and the erase function in C++ lies in their functionality and purpose.

In C++, a notable contrast between the clear and erase functions lies in their respective purposes. The clear function is typically employed to eliminate all elements within the deque, whereas the erase function is commonly used to delete an element from a specified position.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience