By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Which functions change a vectors capacity?

P: n/a
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);
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"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
Jul 19 '05 #2

P: n/a

"Noo" <ar**@HHsys.uea.ac.uk> wrote in message news:bo**********@cpca14.uea.ac.uk...
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.
Jul 19 '05 #3

P: n/a
Noo

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Noo" <ar**@HHsys.uea.ac.uk> wrote in message news:bo**********@cpca14.uea.ac.uk...
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.


Great deduction there! And thanks for the help.

Alan
Jul 19 '05 #4

P: n/a
Noo

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:O0usb.181597$Tr4.505377@attbi_s03...
"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.


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
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.


Well, 'v.clear()' followed by 'cout << v.capacity()' gave the answer 0,
though whether it would always do that...
resize() leaves the capacity as is.


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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.