std::vector clear zeroing capacity

Started by
4 comments, last by jflanglois 17 years, 10 months ago
Hi All, Have searched this, with no luck. I was under the impression that, having filled my vector with objects, i could .clear() it and the underlying capacity should not be changed? I have searched some online vector resources, and almost all of them report that .clear() simply calls .erase(begin(), end()), however, I have done a test and this is not the case. Is my version of the STL (default that is installed with vc7.1) not conforming, or is this expected behaviour? Alex The test I performed is below


	std::vector<int> testVector;

	int iCapacity = testVector.capacity(); //returns 0

	testVector.push_back(5);

	iCapacity = testVector.capacity(); //returns 1

	testVector.resize(10);

	iCapacity = testVector.capacity(); //returns 10

	testVector.pop_back();

	iCapacity = testVector.capacity(); //returns 10

	testVector.erase(testVector.begin(), testVector.end());

	iCapacity = testVector.capacity(); //returns 10

	testVector.resize(10);

	iCapacity = testVector.capacity(); //returns 10

	testVector.clear();

	iCapacity = testVector.capacity(); //RETURNS 0!!!!!


Advertisement
Yep, pain in the ass, huh? :)

The secret is calling resize(0) instead.

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

Thanks for the info.

Any idea whether this is the correct behaviour, or is my implementation of the STL buggy?

Nice game by the way.

Alex
I don't know if it's wrong, but it isn't something I would have expected.

libstdc++ and stlport both give me 10 if you wanted something to compare with it to.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I get the same behaviour with Visual Studio .NET 2003.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Quote:Original post by deathkrush
I get the same behaviour with Visual Studio .NET 2003.

Indeed you do, since that is 7.1.

In VC2005, however, the last call to vector::capacity() returns 10 (which is what I and I am sure many would expect), so it must be something with previous implementations. The standard draft of 1998 (and the working draft of 2005) makes no mention (as far as I can tell) regarding capacity after a call to clear(), the only requirements being that it is equivalent to erase( begin(), end() ) and post size() == 0, so capacity could be 0, I guess.

[edit] For those who are interested, I believe the correct way to ensure a capacity of 0 is the empty-temporary/swap idiom:
std::vector< int > v;
// ...
std::vector< int >().swap( v );


jfl.

This topic is closed to new replies.

Advertisement