ptr_vector<T> vec1;
...
ptr_vector<T> vec2( vec1.clone() ); // deep copy objects of 'vec1' and use them to construct 'vec2', could be very expensive
vec2 = vec1.release(); // give up ownership of pointers in 'vec1' and pass the ownership to 'vec2', rather cheap
vec2.release(); // give up ownership; the objects will be deallocated if not assigned to another container
vec1 = vec2; // compile time error: 'operator=()' not defined
ptr_vector<T> vec3( vec1 ); // compile time error: copy-constructor not defined

This examples shows many of the most common
features at work. The example provide lots of comments.

//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2003-2005. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
//
// This example is intended to get you started.
// Notice how the smart container
//
// 1. takes ownership of objects
// 2. transfers ownership
// 3. applies indirection to iterators
// 4. clones objects from other smart containers
//
//
// First we select which container to use.
//#include<boost/ptr_container/ptr_deque.hpp>//
// we need these later in the example
//#include<boost/assert.hpp>#include<string>#include<exception>//
// Then we define a small polymorphic class
// hierarchy.
//classanimal:boost::noncopyable{virtualstd::stringdo_speak()const=0;std::stringname_;protected://
// Animals cannot be copied...
//animal(constanimal&r):name_(r.name_){}voidoperator=(constanimal&);private://
// ...but due to advances in genetics, we can clone them!
//virtualanimal*do_clone()const=0;public:animal(conststd::string&name):name_(name){}virtual~animal()throw(){}std::stringspeak()const{returndo_speak();}std::stringname()const{returnname_;}animal*clone()const{returndo_clone();}};//
// An animal is still not Clonable. We need this last hook.
//
// Notice that we pass the animal by const reference
// and return by pointer.
//animal*new_clone(constanimal&a){returna.clone();}//
// We do not need to define 'delete_clone()' since
// since the default is to call the default 'operator delete()'.
//conststd::stringmuuuh="Muuuh!";conststd::stringoiink="Oiiink";classcow:publicanimal{virtualstd::stringdo_speak()const{returnmuuuh;}virtualanimal*do_clone()const{returnnewcow(*this);}public:cow(conststd::string&name):animal(name){}};classpig:publicanimal{virtualstd::stringdo_speak()const{returnoiink;}virtualanimal*do_clone()const{returnnewpig(*this);}public:pig(conststd::string&name):animal(name){}};//
// Then we, of course, need a place to put all
// those animals.
//classfarm{//
// This is where the smart containers are handy
//typedefboost::ptr_deque<animal>barn_type;barn_typebarn;//
// An error type
//structfarm_trouble:publicstd::exception{};public://
// We would like to make it possible to
// iterate over the animals in the farm
//typedefbarn_type::iteratoranimal_iterator;//
// We also need to count the farm's size...
//typedefbarn_type::size_typesize_type;//
// And we also want to transfer an animal
// safely around. The easiest way to think
// about '::auto_type' is to imagine a simplified
// 'std::auto_ptr<T>' ... this means you can expect
//
// T* operator->()
// T* release()
// deleting destructor
//
// but not more.
//typedefbarn_type::auto_typeanimal_transport;//
// Create an empty farm.
//farm(){}//
// We need a constructor that can make a new
// farm by cloning a range of animals.
//farm(animal_iteratorbegin,animal_iteratorend)://
// Objects are always cloned before insertion
// unless we explicitly add a pointer or
// use 'release()'. Therefore we actually
// clone all animals in the range
//barn(begin,end){}//
// ... so we need some other function too
//animal_iteratorbegin(){returnbarn.begin();}animal_iteratorend(){returnbarn.end();}//
// Here it is quite ok to have an 'animal*' argument.
// The smart container will handle all ownership
// issues.
//voidbuy_animal(animal*a){barn.push_back(a);}//
// The farm can also be in economical trouble and
// therefore be in the need to sell animals.
//animal_transportsell_animal(animal_iteratorto_sell){if(to_sell==end())throwfarm_trouble();//
// Here we remove the animal from the barn,
// but the animal is not deleted yet...it's
// up to the buyer to decide what
// to do with it.
//returnbarn.release(to_sell);}//
// How big a farm do we have?
//size_typesize()const{returnbarn.size();}//
// If things are bad, we might choose to sell all animals :-(
//std::auto_ptr<barn_type>sell_farm(){returnbarn.release();}//
// However, if things are good, we might buy somebody
// else's farm :-)
//voidbuy_farm(std::auto_ptr<barn_type>other){//
// This line inserts all the animals from 'other'
// and is guaranteed either to succeed or to have no
// effect
//barn.transfer(barn.end(),// insert new animals at the end*other);// we want to transfer all animals,
// so we use the whole container as argument
//
// You might think you would have to do
//
// other.release();
//
// but '*other' is empty and can go out of scope as it wants
//BOOST_ASSERT(other->empty());}};// class 'farm'.intmain(){//
// First we make a farm
//farmanimal_farm;BOOST_ASSERT(animal_farm.size()==0u);animal_farm.buy_animal(newpig("Betty"));animal_farm.buy_animal(newpig("Benny"));animal_farm.buy_animal(newpig("Jeltzin"));animal_farm.buy_animal(newcow("Hanz"));animal_farm.buy_animal(newcow("Mary"));animal_farm.buy_animal(newcow("Frederik"));BOOST_ASSERT(animal_farm.size()==6u);//
// Then we make another farm...it will actually contain
// a clone of the other farm.
//farmnew_farm(animal_farm.begin(),animal_farm.end());BOOST_ASSERT(new_farm.size()==6u);//
// Is it really clones in the new farm?
//BOOST_ASSERT(new_farm.begin()->name()=="Betty");//
// Then we search for an animal, Mary (the Crown Princess of Denmark),
// because we would like to buy her ...
//typedeffarm::animal_iteratoriterator;iteratorto_sell;for(iteratori=animal_farm.begin(),end=animal_farm.end();i!=end;++i){if(i->name()=="Mary"){to_sell=i;break;}}farm::animal_transportmary=animal_farm.sell_animal(to_sell);if(mary->speak()==muuuh)//
// Great, Mary is a cow, and she may live longer
//new_farm.buy_animal(mary.release());else//
// Then the animal would be destroyed (!)
// when we go out of scope.
//;//
// Now we can observe some changes to the two farms...
//BOOST_ASSERT(animal_farm.size()==5u);BOOST_ASSERT(new_farm.size()==7u);//
// The new farm has however underestimated how much
// it cost to feed Mary and its owner is forced to sell the farm...
//animal_farm.buy_farm(new_farm.sell_farm());BOOST_ASSERT(new_farm.size()==0u);BOOST_ASSERT(animal_farm.size()==12u);}