C ++ - how to create a data structure so that it can avoid memory leak

Possible duplicate:
object pointer vector - how to avoid memory leak?

Hello everybody,

I have the following code,

vector<Student*> vec;
// populate vec with Student pointers

Then sort(vec.begin(), vec.end(), ...)sort the student based on grade.

Here is the question

Since the vector stores pointers, we must later remember that we need to delete it manually. However, we cannot store auto_ptr inside the vector.

Is there a better way to do this? Store smart pointer inside a vector? How to create this smart pointer?

thank

+3
source share
3 answers

Colin - auto_ptr , . , , : , . Boost , shared_ptr intrusive_ptr. , ( ) boost:: shared_ptr - , .

+2

: smrtpntr, - , , .

+1

On the flip side of using smart pointer vector, as Colin and Mark D. suggested, you can also use boost :: ptr_vector

+1
source

All Articles