#include // for EXIT_SUCCESS #include // for std::cout, std::endl #include // for std::unique_ptr #include // for std::vector using std::unique_ptr; using std::vector; using std::cout; using std::endl; int main(int argc, char** argv) { vector > vec; vec.push_back(unique_ptr(new int(9))); vec.push_back(unique_ptr(new int(5))); vec.push_back(unique_ptr(new int(7))); // z is (a copy of) the (int pointed to by // the unique_ptr in vec[1]) int z = *vec[1]; cout << "z is: " << z << endl; cout << "vec[1] is: " << *vec[1] << endl; // compiler error, since unique_ptrs can't be copied //unique_ptr copied(vec[1]); // works, but now vec[1] has a null pointer. unique_ptr moved = std::move(vec[1]); cout << "*moved: " << *moved << endl; cout << "z is: " << z << endl; // segmentation fault, since vec[1]==nullptr after move //cout << "vec[1] is: " << *vec[1] << endl; return EXIT_SUCCESS; }