Discussion:
vector, reserve, resize?
(too old to reply)
Jeremy Pullicino
2003-09-26 09:39:56 UTC
Permalink
I have a set which I wish to copy into a vector (which is initially empty)

I am using std::copy to accomplish this.

Before the copy, I am using the resize member of vector to resize the vector
to the size of the set.

I am unsure however if I should additionaly be using reserve.

Should I call reserve before calling resize?

What is the use of reserve?

Jeremy.
André Pönitz
2003-09-26 09:57:41 UTC
Permalink
Post by Jeremy Pullicino
I have a set which I wish to copy into a vector (which is initially empty)
I am using std::copy to accomplish this.
Why not simply use the appropriate vector constructor

std::set<int> s;
// fill it
std::vector<int> v(s.begin(), s.end());

?

Should be faster than any combination of resize/reserve and manual
copying.

Andre'
--
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
Jeremy Pullicino
2003-09-26 13:15:54 UTC
Permalink
I did not know that the constructor was faster - thanks for letting me know.

However, I cannot do this anyway since I will not know the size of the
vector when it is constructed, I will only know it later.

Jeremy.
Post by André Pönitz
Post by Jeremy Pullicino
I have a set which I wish to copy into a vector (which is initially empty)
I am using std::copy to accomplish this.
Why not simply use the appropriate vector constructor
std::set<int> s;
// fill it
std::vector<int> v(s.begin(), s.end());
?
Should be faster than any combination of resize/reserve and manual
copying.
Andre'
--
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
André Pönitz
2003-09-26 13:47:07 UTC
Permalink
Post by Jeremy Pullicino
I did not know that the constructor was faster - thanks for letting me know.
However, I cannot do this anyway since I will not know the size of the
vector when it is constructed, I will only know it later.
You do not need the size of the set explicitly when creating the vector
using that constructor... Have you tried what I proposed?

Andre'
Bo Persson
2003-09-26 15:39:45 UTC
Permalink
Post by Jeremy Pullicino
I did not know that the constructor was faster - thanks for letting me know.
However, I cannot do this anyway since I will not know the size of the
vector when it is constructed, I will only know it later.
Why don't you wait till you know the size before creating the vector?

Anyway, if you want to assign something to a vector (replacing any
previous content), you can use

v.assign(s.begin(), s.end());

which works similar to the constructor.




Bo Persson
Post by Jeremy Pullicino
Jeremy.
Post by André Pönitz
Post by Jeremy Pullicino
I have a set which I wish to copy into a vector (which is
initially
Post by Jeremy Pullicino
empty)
Post by André Pönitz
Post by Jeremy Pullicino
I am using std::copy to accomplish this.
Why not simply use the appropriate vector constructor
std::set<int> s;
// fill it
std::vector<int> v(s.begin(), s.end());
?
Should be faster than any combination of resize/reserve and manual
copying.
Andre'
--
Those who desire to give up Freedom in order to gain Security,
will not
Post by Jeremy Pullicino
have,
Post by André Pönitz
nor do they deserve, either one. (T. Jefferson or B. Franklin
or
Post by Jeremy Pullicino
both...)
Craig Powers
2003-09-29 22:11:42 UTC
Permalink
Post by Jeremy Pullicino
I have a set which I wish to copy into a vector (which is initially empty)
I am using std::copy to accomplish this.
Before the copy, I am using the resize member of vector to resize the vector
to the size of the set.
I am unsure however if I should additionaly be using reserve.
Should I call reserve before calling resize?
No. In this case, you set the size of the vector, so it only requires
one allocation. Reserve is helpful when you will be doing multiple
appends to the vector.
Post by Jeremy Pullicino
What is the use of reserve?
Reserve is useful if you want to pre-allocate a certain amount of
space to avoid the performance hit from resizing the allocated space
as the vector grows.
--
Craig Powers
MVP - Visual C++
tom_usenet
2003-09-30 10:17:12 UTC
Permalink
Post by Craig Powers
Post by Jeremy Pullicino
I have a set which I wish to copy into a vector (which is initially empty)
I am using std::copy to accomplish this.
Before the copy, I am using the resize member of vector to resize the vector
to the size of the set.
I am unsure however if I should additionaly be using reserve.
Should I call reserve before calling resize?
No. In this case, you set the size of the vector, so it only requires
one allocation. Reserve is helpful when you will be doing multiple
appends to the vector.
Post by Jeremy Pullicino
What is the use of reserve?
Reserve is useful if you want to pre-allocate a certain amount of
space to avoid the performance hit from resizing the allocated space
as the vector grows.
It also has the semantic benefit of ensuring that iterators,
references and pointers to vector elements remain valid during appends
as long as the capacity isn't exceeded.

Tom
Carl Daniel [VC++ MVP]
2003-10-01 04:04:37 UTC
Permalink
Post by tom_usenet
Post by Craig Powers
Post by Jeremy Pullicino
What is the use of reserve?
Reserve is useful if you want to pre-allocate a certain amount of
space to avoid the performance hit from resizing the allocated space
as the vector grows.
It also has the semantic benefit of ensuring that iterators,
references and pointers to vector elements remain valid during appends
as long as the capacity isn't exceeded.
Additionally, in contrast to reserve, resize will construct all of the
elements that are appended to the vector (recall that the signature for
reseize is

void resize(size_type sz, T c = T());

so one should use reserve when the desire is to avoid unnecessary
intermediate reallocations before doing bulk insertions, as it avoids the
wasted cost of default-constructing all the new elements (which will
subsequently be copied over).

23.2.4.3(1) implies that a conforming implementation of
std::vector::insert(iterator, iterator) will effectively call reserve before
the insertions, so there's no need to call reserve (or resize) before using
the iterator-pair form of insert..

-cd

Loading...