Which functions change a vectors capacity?



 DEVELOP > c-Plus-Plus > Which functions change a vectors capacity?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Noo"
Date: 12 Nov 2003 05:54:29 AM
Object: Which functions change a vectors capacity?
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);
.
(Maybe inserting up to 1000 elements, maybe not)
.
vector<int> second;
second.reserve(1000);
second = first;
the capacity of second need not be 1000.
I was just wondering what other capacity changing functions I need to beware
of!
Thanks,
Alan
.

User: "Ron Natalie"

Title: Re: Which functions change a vectors capacity? 12 Nov 2003 11:33:38 AM
"Noo" <arGG@HHsys.uea.ac.uk> wrote in message news:botp4q$ti6$1@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.
.
User: "Noo"

Title: Re: Which functions change a vectors capacity? 12 Nov 2003 12:04:20 PM
"Ron Natalie" <ron@sensor.com> wrote in message
news:3fb26ef3$0$25241$9a6e19ea@news.newshosting.com...


"Noo" <arGG@HHsys.uea.ac.uk> wrote in message

news:botp4q$ti6$1@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
.


User: "Victor Bazarov"

Title: Re: Which functions change a vectors capacity? 12 Nov 2003 11:23:58 AM
"Noo" <arGG@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
.
User: "Noo"

Title: Re: Which functions change a vectors capacity? 12 Nov 2003 12:11:24 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:O0usb.181597$Tr4.505377@attbi_s03...

"Noo" <arGG@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
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER