Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::vector v. std::tr1::array v. std::tr1::shared_pointer v. boost::ptr_vector<Y>

75 views
Skip to first unread message

Siegfried Heintze

unread,
Jan 8, 2009, 11:50:43 PM1/8/09
to
Can someone help me identify some criteria for selecting between
std::vector, std::tr1::array and using a custom deleter and
boost::ptr_vector<Y>?

Thanks,
Siegfried


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Sean Hunt

unread,
Jan 9, 2009, 6:08:19 PM1/9/09
to
On Jan 8, 9:50 pm, "Siegfried Heintze" <siegfr...@heintze.com> wrote:
> Can someone help me identify some criteria for selecting between
> std::vector, std::tr1::array and using a custom deleter and
> boost::ptr_vector<Y>?
>
> Thanks,
> Siegfried

std::tr1::array<T, N> extremely closely mimics a C array T[N]. It
should be used when you are using fixed-length data, or have some
other reason to fix your memory at compile time. It should be used in
place of a C array whenever possible.

boost::ptr_vector<T> exists as a replacement for std::vector<T*> when
you want the ptr_vector to automatically call the deleter on the
contained objects. It also has the advantage of providing a slightly
nicer interface - operator[] dereferences the pointer, for instance.
With a no-op deleter, it is essentially the same as vector except that
it that slightly nicer interface.

std::vector<T> is for the remainder of cases, but you should note that
there is also std::list and std::deque, which provide different
options for container use. In particular, deque is oftentimes a better
choice in cases where vector is used - it is very similar, but it does
not provide contiguous allocation. In return, however, it provides
constant-time insertions and deletions at either end of the sequence,
which can be very useful compared to a vector.

Sean

Mathias Gaunard

unread,
Jan 9, 2009, 6:15:36 PM1/9/09
to
On Jan 9, 5:50 am, "Siegfried Heintze" <siegfr...@heintze.com> wrote:
> Can someone help me identify some criteria for selecting between
> std::vector, std::tr1::array and using a custom deleter and
> boost::ptr_vector<Y>?

array is for statically sized collections, while vector is for
dynamically sized and resizable ones.

ptr_vector is a wrapper around vector when you're holding pointers and
must own them. Ideally this shouldn't be needed: a vector of
unique_ptr provides the same functionality, except it requires C++0x.

0 new messages