based objects. return 0;
#include <stl.h> #include <iostream.h>
class X { public: X (int i_) : i (i_) {} ~X () { cout << "Delete X(" << i << ")" << endl; } int i; };
ostream& operator << (ostream& stream_, const X& x_) { return stream_ << "X(" << x_.i << ")"; }
int main () { vector<X*> v; v.push_back (new X (2)); v.push_back (new X (1)); v.push_back (new X (4)); vector<X*>::iterator i; for (i = v.begin (); i != v.end (); i++) cout << *(*i) << endl; release (v.begin (), v.end ()); // Delete heap- based objects. return 0; }
#include <stl.h> #include <iostream.h>
class X { public: X (int i_) : i (i_) {} ~X () { cout << "Delete X(" << i << ")" << endl; } int i; };
ostream& operator << (ostream& stream_, const X& x_) { return stream_ << "X(" << x_.i << ")"; }
int main () { vector<X*> v; v.push_back (new X (2)); v.push_back (new X (1)); v.push_back (new X (4)); vector<X*>::iterator i; cout << "Initial contents:" << endl; for (i = v.begin (); i != v.end (); i++) cout << " " << *(*i) << endl; release (v.begin ()); // Delete the first heap-based object. v.erase (v.begin ()); // Erase the first element. cout << "Remaining contents:" << endl; for (i = v.begin (); i != v.end (); i++) cout << " " << *(*i) << endl; release (v.begin (), v.end ()); // Delete remaining heap-based objects. v.erase (v.begin (), v.end ()); // Erase remaining elements. return 0; }