Boost logo

Boost Users :

From: Matt S Trentini (matt_s_trentini_at_[hidden])
Date: 2003-11-11 08:00:17

Hi Dave,

Vector is more flexible than array. But this flexibility comes at a cost.
Even if you reserve space for a known number of elements, vector cannot
assume that you won't add or remove elements. So there's a minor overhead
in keeping track of when it needs to expand if you choose to add more

Also, reserving elements for a vector only guarantees that space for _at
least_ that number of elements is reserved (see in the standard) -
it may in fact reserve more in the hope that it won't need to reallocate in
the event you choose to add more elements. So it may consume more memory.

Boost::array can avoid all of those potential issues because you specify the
number of elements at compile-time and it cannot change.

But the main reason to use boost::arrays IMHO is for clarity. If you know
how many elements are required they let you express that clearly - something
that neither vector nor standard arrays can do. This is of particular
advantage when you consider passing a container with n elements to a

  void thisFunctionRequiresA10ElementArray(const boost::array<T, 10>
  void notSureHowManyElementsThisNeeds(const std::vector<T> elements);

Hope that helps!


PS Glad to provide you with a nice $100 bill... ;)

"Dave" <better_cs_now_at_[hidden]> wrote in message
> Hello all,
> I have recently reviewed the Boost presentation put together by Matthew
> intend to present it to my peers also. It was especially welcome to see
> this because I've been wanting to present to my peers on Boost for awhile
> but haven't had the time to learn enough about the library to put a
> presentation together. So, something I wanted fell right in my lap!
> like finding a $100 bill on the street!
> I do have one question about the array library. In the documentation, the
> statement is made that the advantage array has over vector is the fact
> since vectors provide the semantics of dynamic arrays, "This results in
> overhead in case only arrays with static sizes are needed.". When
> discussing this with my peers, I'd like to present something a little more
> concrete to them.
> If I have a number of elements that is known at compile time, why,
> *specifically*, should I prefer boost::array to std::vector?
> Thanks!
> Dave

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at