Map clear()


Hi all,

This is the description for map clear() function in wikipedia..

vector::clear - Erases all of the elements. (For most STL implementations this is O(1) time and does not reduce capacity)

Whats is your opinion about this?? does this really not reduce the size of the map??


Thanks in advance
Shiva
size and capacity are two different things.

capacity is how much space you have allocated, and size is how much of that space is actually used.

clear will set the size to 0, but probably doesn't change capacity (ie: it doesn't actually free the memory). This isn't guaranteed, though.

In any event, that's for vector. map works differently (it's more like a list with individual nodes rather than an array) so it doesn't really have a capacity.

Thanks a lot

what happens if clear map/vector, does the capacity remains same even after clear??
Exact behavior isn't specified by the standard. It depends on the implementation.

It doesn't really matter, anyway. If you're really worried that much about memory consumption, you can make the vector with new and then delete it when you want to free all the memory.

Again... also note that map doesn't really have a capacity because it's based on individual linked nodes and not an array.
Last edited on
Interestingly, my friend had a large vector that he tried to do that with in Debug mode in VC++, and when he deleted it and remade it (to reset it's capacity), it had the same capacity as it had before. Exactly the same (10,000 IIRC). We weren't really sure why that happened...
Thanks a lot Disch.
Topic archived. No new replies allowed.