is vec.reserve(unsigned int) better than vec(unsigned int)?

E

er

hi,

of A and B below i have a feeling A is more efficient although i'm
used to doing A unless i can't... could someone kindly confirm/refute?

//A
vector<T> vec;
vec.reserve(n);//only allocates mem.
transform(...,back_inserter(vec));
//B
vector<T> vec(n);//uses T's default constructor to fill the vector
transform(...,vec.begin());
 
V

Victor Bazarov

er said:
of A and B below i have a feeling A is more efficient although i'm
used to doing A unless i can't... could someone kindly confirm/refute?

//A
vector<T> vec;
vec.reserve(n);//only allocates mem.
transform(...,back_inserter(vec));
//B
vector<T> vec(n);//uses T's default constructor to fill the vector
transform(...,vec.begin());

It's impossible to tell. Measure, don't "have a feeling".

V
 
A

Andre Kostur

with reserve you are guaranteed atleast n , with vec(n) u just hv n
places and nothing more

Really? Where in the Standard does it specify that after vec(n) that
vec.capacity() == n ? (And get your keyboard replaced... it is
apparently dropping letters.)
 
D

digz

@y42g2000hsy.googlegroups.com:





Really? Where in the Standard does it specify that after vec(n) that
vec.capacity() == n ? (And get your keyboard replaced... it is
apparently dropping letters.)

Andre you made me rethink ( this is from g++-3.4.4 source)
but I still feel the same about vector v(n)..may be g++ is not
standard compliant ?
Correct me if I am wrong

particularly this line :
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;

-----
explicit
vector(size_type __n)
: _Base(__n, allocator_type())
{ this->_M_impl._M_finish = std::uninitialized_fill_n(this-
_M_impl._M_start,
__n, value_type()); }
_Vector_base(size_t __n, const allocator_type& __a)
: _M_impl(__a)
{
this->_M_impl._M_start = this->_M_allocate(__n);
this->_M_impl._M_finish = this->_M_impl._M_start;
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
}
 
A

Andre Kostur

Andre you made me rethink ( this is from g++-3.4.4 source)
but I still feel the same about vector v(n)..may be g++ is not
standard compliant ?
Correct me if I am wrong

particularly this line :
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;

-----
explicit
vector(size_type __n)
: _Base(__n, allocator_type())
{ this->_M_impl._M_finish = std::uninitialized_fill_n(this-
__n, value_type()); }
_Vector_base(size_t __n, const allocator_type& __a)
: _M_impl(__a)
{
this->_M_impl._M_start = this->_M_allocate(__n);
this->_M_impl._M_finish = this->_M_impl._M_start;
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
}

This doesn't invalidate the question. AFAIK the standard doesn't say
anything about the capacity() of vector after "vector v(n);" Which
basically leaves it a capacity() >= size(). capacity() == size() is a
valid implementation, but it's not a requirement.
 

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
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top