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

Discussion in 'C++' started by er, Sep 13, 2007.

  1. er

    er Guest

    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());
     
    er, Sep 13, 2007
    #1
    1. Advertising

  2. er wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 13, 2007
    #2
    1. Advertising

  3. er

    digz Guest

    On Sep 13, 6:00 pm, "Victor Bazarov" <> wrote:
    > er wrote:
    > > 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
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    with reserve you are guaranteed atleast n , with vec(n) u just hv n
    places and nothing more
     
    digz, Sep 14, 2007
    #3
  4. er

    Andre Kostur Guest

    digz <> wrote in news:1189725878.992881.254880
    @y42g2000hsy.googlegroups.com:

    > On Sep 13, 6:00 pm, "Victor Bazarov" <> wrote:
    >> er wrote:
    >> > 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".

    >
    > 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.)
     
    Andre Kostur, Sep 14, 2007
    #4
  5. er

    er Guest

    On Sep 13, 7:24 pm, digz <> wrote:
    > On Sep 13, 6:00 pm, "Victor Bazarov" <> wrote:
    >
    >
    >
    > > er wrote:
    > > > 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
    > > --
    > > Please remove capital 'A's when replying by e-mail
    > > I do not respond to top-posted replies, please don't ask

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


    thanks.
     
    er, Sep 14, 2007
    #5
  6. er

    digz Guest

    On Sep 13, 7:59 pm, Andre Kostur <> wrote:
    > digz <> wrote in news:1189725878.992881.254880
    > @y42g2000hsy.googlegroups.com:
    >
    >
    >
    > > On Sep 13, 6:00 pm, "Victor Bazarov" <> wrote:
    > >> er wrote:
    > >> > 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".

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


    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;
    }
     
    digz, Sep 14, 2007
    #6
  7. er

    Andre Kostur Guest

    digz <> wrote in
    news::

    > On Sep 13, 7:59 pm, Andre Kostur <> wrote:
    >> digz <> wrote in news:1189725878.992881.254880
    >> @y42g2000hsy.googlegroups.com:
    >>
    >>
    >>
    >> > On Sep 13, 6:00 pm, "Victor Bazarov" <>
    >> > wrote:
    >> >> er wrote:
    >> >> > 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".

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

    >
    > 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;
    > }
    >
    >


    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.
     
    Andre Kostur, Sep 14, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    987
    Bob Hairgrove
    Oct 30, 2004
  2. Peter Bencsik
    Replies:
    2
    Views:
    868
  3. Ioannis Vranos

    (&vec)== &vec[0]?

    Ioannis Vranos, Sep 30, 2008, in forum: C++
    Replies:
    5
    Views:
    441
    Juha Nieminen
    Oct 1, 2008
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    434
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    784
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page