Dhruv Joshi
Dhruv Joshi

Reputation: 53

Why and when to use clear()

According to this clear() will destroy all the objects, but will not free the memory. Also while I encountered the necessity of removing all elements of a set I found out that the time complexity of set.clear() is linear in size (destructions) whereas constant for set.swap(). (same problem while removing all elements of vector)

So isn't that we should always use swap() with an empty STL container whenever possible in such cases? Thus what is the need and benifits of clear() over swap with an empty container.

Sorry, if this seems trivial but I couldn't find anything regarding this. Thanks!

Upvotes: 1

Views: 2013

Answers (3)

eerorika
eerorika

Reputation: 238421

when to use clear()

When you want to remove all elements of a container.

why use clear()

Because it is the clearest and most efficient way to achieve the above.

clear() will destroy all the objects, but will not free the memory.

This is a desirable feature. It allows clear to be fast, and allows addition of new elements into the container to be fast because memory has already been allocated.

If you want to free the memory - knowing that you will need to pay again for the allocation if you insert new elements, you can use shrink_to_fit.

I found out that the time complexity of set.clear() is linear in size (destructions) whereas constant for set.swap().

You are forgetting that the previously empty, now full set has to be eventually destroyed. Guess what the complexity of the destructor of set is.

It is linear in size of the set.

So isn't that we should always use swap() with an empty STL container whenever possible in such cases?

Swap introduces extra unnecessary operations which clear does not do, and is therefore not as efficient. Furthermore, it does not express the intent as clearly.

There is no advantage in replacing clear with a swap in general case.


Swapping with an empty container does have its uses in rare cases. For example, if you need to clear a container within a critical section, then swapping and leaving the destruction of elements later can allow reducing the size of that critical section.

Upvotes: 5

Jeremy Friesner
Jeremy Friesner

Reputation: 73219

Certainly calling swap() will be faster, but unless you've got an infinite supply of empty containers available, eventually you will have to do something with the container(s) you've swap()'d items into, in which case you're back to destroying items in linear time.

That doesn't mean the swap() trick isn't useful, though -- for example, if you have a program with a real-time thread and a non-real-time thread, you might swap() the container in the real-time thread, and leave it up to the non-real-time thread to do the actual clear()/items-destruction later on, so that the real-time thread doesn't ever have to spend its limited time destroying items.

Upvotes: 4

JaMiT
JaMiT

Reputation: 17005

The time complexity of swap() is constant, yes.

Presumably you are thinking of swapping with a local variable that soon goes out of scope. If this is the case, the destructor of the local variable is called as it goes out of scope, and the time complexity of that destructor is linear in size, the same as clear() would have been. So there is no performance benefit, and you pay the price of less readable code.

Upvotes: 4

Related Questions