This is always a question.
First of all, it really depends on whether your C ++ 11 compiler supports move semantics or not, as this dramatically changes aspects of the problem.
For those stuck in C ++ 03
There are several options:
std::list<MyClass> list; list.push_front(MyClass());
Even if there is a semantically copy, the optimizer can delete most of the backup / dead storage. Most optimizers will require a default constructor definition and copy constants.
boost::ptr_deque<MyClass> deque; std::auto_ptr<MyClass> p(new MyClass()); deque.push_front(p);
ptr_vector can be used if you replace push_front with push_back , otherwise it is a little wasteful. This avoids most of the memory overhead std::list<MyClass*> and has the added bonus of automatically processing memory.
boost::stable_vector<MyClass> svec; svec.push_back(MyClass());
There is one copy (like the list), but the guarantee that no additional copy (like with the list) should be made in the container. It also allows you to perform several operations than a list (for example, random access), due to a slower nesting in the middle for large containers.
For those who use C ++ 11
std::list<MyClass> list; list.push_front(MyClass());
does not create any copy; instead, a move operation occurs.
It is also possible to use new operations to create objects:
std::list<MyClass> list; list.emplace_front();
will create a new MyClass directly inside the node, without copying, without moving.
And finally, you may wish for a more compact view or other operations on the container, in which case:
std::vector<std::unique_ptr<MyClass>> vec; vec.emplace_back(new MyClass());
Offers you random access and lower overhead.