Boost logo

Boost Users :

From: Dave (better_cs_now_at_[hidden])
Date: 2003-11-11 15:51:54


> 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
> elements.
>
> Also, reserving elements for a vector only guarantees that space for _at
> least_ that number of elements is reserved (see 23.2.4.2 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
> function:
>
> void thisFunctionRequiresA10ElementArray(const boost::array<T, 10>
> tenElements);
> void notSureHowManyElementsThisNeeds(const std::vector<T> elements);
>
> Hope that helps!
>
> Cheers,
> Matt
>
> PS Glad to provide you with a nice $100 bill... ;)

Hmmm, I suppose my first question leads naturally to another...

Why would one prefer a tuple to a simple struct?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net