Pointers and containers

We all know that RAW pointers need to be wrapped in some form of smart pointer to get Exception safe memory management. But when it comes to containers of pointers the issue becomes more thorny.

The std containers insist on the contained object being copyable so this rules out the use of std::auto_ptr, though you can still use boost::shared_ptr etc.

But there are also some boost containers designed explicitly to hold pointers safely: See Pointer Container Library

The question is: Under what conditions should I prefer to use the ptr_containers over a container of smart_pointers?



std::vector<boost::shared_ptr<X> >


Boost pointer containers have strict ownership over the resources they hold. A std::vector<boost::shared_ptr<X>> has shared ownership. There are reasons why that may be necessary, but in case it isn't, I would default to boost::ptr_vector<X>. YMMV.

Need Your Help

I am looking for an elegant means of extracting nested data from a MATLAB data structure

arrays matlab struct nested-lists

Using MATLAB, other than the brute force technique of using nested FOR loops, I am curious if there is a more elegant means of extracting the X &amp; Y data from the sample data structure that I have