"Noo" <ar**@HHsys.uea.ac.uk> wrote...
Hi. I've got some code that uses vectors fairly extensively and it needs
to be efficient. Therefore I'm using reserve() quite a bit. However what
other functions (are supposed to) change the capacity?
All that may lead to 'insert', if such change is necessary.
I know that adding extra elements (e.g. by using push_back()) may do this,
if extra capacity is needed.
Why do you think you actually need the capacity? The whole idea
of 'reserving' some space is to [try to] avoid reallocations if
'push_back' is used.
On the C++ implementation I'm using:
clear() seems to reset the capacity to zero.
Even if it seems to, it doesn't necessarily do so.
resize() leaves the capacity as is.
Depends on whether you resize it beyond current capacity or not.
operator=() does not copy the capacity.
There is no requirement that it should.
I thought for a while that, if
the elements fit into the vector on the left hand side of the = sign, the
capacity of this vector would remain the same, but copying an empty vector
seems to reset it to zero. So that after
vector<int> first;
first.reserve(1000);
.
(Maybe inserting up to 1000 elements, maybe not)
.
vector<int> second;
second.reserve(1000);
second = first;
the capacity of second need not be 1000.
Correct. There is no such requirement in the language.
I was just wondering what other capacity changing functions I need to
beware of!
'erase', 'insert', 'resize' (which is just an erase or an insert
depending on the current and required size), 'push_back' and
'push_front' (which are just short-cuts for 'insert'), all can
change the capacity of a vector.
Perhaps you need to explain what _problem_ you're trying to solve.
And, perhaps, getting and reading a good book on Standard Library
would help. I recommend the one by Nicolai Josuttis.
Victor