Which functions change a vectors capacity?

N

Noo

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?

I know that adding extra elements (e.g. by using push_back()) may do this,
if extra capacity is needed.

On the C++ implementation I'm using:
clear() seems to reset the capacity to zero.
resize() leaves the capacity as is.
operator=() does not copy the capacity. 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);
 
V

Victor Bazarov

Noo said:
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
 
R

Ron Natalie

Noo said:
I know that adding extra elements (e.g. by using push_back()) may do this,
if extra capacity is needed.

It is very uncommon for anything to shirnk capacity. reserve is NOT supposed
to. Most of the other changes aren't supposed to cause a reallocation (which you
would effectively have to do to shirnk capacity). I suspect from the description of
the behavior you are using VC++ 7. Microsoft says this behavior will go away in
the next release.

"swap()" will generally take the capacity with the swapped elements.
 
N

Noo

Ron Natalie said:
It is very uncommon for anything to shirnk capacity. reserve is NOT supposed
to. Most of the other changes aren't supposed to cause a reallocation (which you
would effectively have to do to shirnk capacity). I suspect from the description of
the behavior you are using VC++ 7. Microsoft says this behavior will go away in
the next release.

Great deduction there! And thanks for the help.

Alan
 
N

Noo

Victor Bazarov said:
Noo said:
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.

Understood. The problem I was having was that, after reserving space in the
vector, some other call was reducing the capacity. I only noticed when the
profiler showed me how much time was being taken by push_back() calls
Even if it seems to, it doesn't necessarily do so.

Well, 'v.clear()' followed by 'cout << v.capacity()' gave the answer 0,
though whether it would always do that...
Depends on whether you resize it beyond current capacity or not.

Sorry. Meant to write resize(0).
And, perhaps, getting and reading a good book on Standard Library
would help. I recommend the one by Nicolai Josuttis.

Indeed, it's a great book! I'd be posting here more often without it!

Thanks for the help,

Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top