[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] sletning af stl vector indeholdende dynamiskallokeret array



Det er muligt at have vektorer med pointere til
dynamisk allokerede objekter, uden leaks.

Hans-Christian Stadler
---------------------------------

#include <vector>
#include <memory>
#include <exception>
#include <iostream>
#include <algorithm>

using namespace std;

namespace {
	class str_ptr : auto_ptr<string> {
	public:
		str_ptr (string* str) : auto_ptr<string>(str) {}
		str_ptr (const str_ptr& sptr) : auto_ptr<string>(const_cast<str_ptr&>(sptr)) {}
		operator string* (void) { return get(); }
	};

	struct print : public unary_function<const string*,void> {
		void operator() (const string *str) const { cout << *str << ' '; }
	};
}

int main (int argc, char argv[]) {
	typedef vector<str_ptr> myvector;
	try {
		myvector *vec = new myvector;
		do {
			vec->push_back(new string("Bliver"));
			vec->push_back(new string("jeg"));
			vec->push_back(new string("leaket"));
			vec->push_back(new string("??"));
			for_each(vec->begin(), vec->end(), print());
			cout << endl;
			vec->clear();
		} while (true);
		delete vec;
	}
	catch (bad_alloc) {
		cerr << "Some string allocation failed" << endl;
		exit(1);
	}
	return 0;
}


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *