question related to stl vector and boost::shared_ptr

Z

zl2k

hi, all
I have some questions about stl vector.
Suppose I have

std::vector<std::vector<int> > v;
//populate v
//now I want to clear v, should I just write:
v.clear();
//or
for (int i = 0; i < v.size(); i++)
v.clear();
v.clear();

What if I have a vector of objects, say
std::vector<boost::shared_ptr<obj> > v;
//populate v
//now I want to clear v, I guess I have to do
for (int i = 0; i < v.size(); i++)
v.reset();
v.clear();
//or maybe I can just do v.clear() since the boost::shared_ptr are
smart pointers and will destroy by itself after v.clear() is excuted?

Thanks for help.

zl2k
 
T

Thomas Tutone

zl2k wrote:

I have some questions about stl vector.
Suppose I have

std::vector<std::vector<int> > v;
//populate v
//now I want to clear v, should I just write:
v.clear();
Yes.

//or
for (int i = 0; i < v.size(); i++)
v.clear();


Not necessary - Each component vector's destructor will clear that
vector automatically (after all, that's what destruction is all about).
v.clear();

Still have to do this, though.
What if I have a vector of objects, say
std::vector<boost::shared_ptr<obj> > v;
//populate v
//now I want to clear v, I guess I have to do
for (int i = 0; i < v.size(); i++)
v.reset();


Again, not necessary - the whole point of smart pointers like
boost::shared_ptr is to do this work for you.
v.clear();

That takes care of it.

Best regards,

Tom
 
Z

zl2k

Thomas said:
zl2k wrote:

I have some questions about stl vector.
Suppose I have

std::vector<std::vector<int> > v;
//populate v
//now I want to clear v, should I just write:
v.clear();
Yes.

//or
for (int i = 0; i < v.size(); i++)
v.clear();


Not necessary - Each component vector's destructor will clear that
vector automatically (after all, that's what destruction is all about).
v.clear();

Still have to do this, though.
What if I have a vector of objects, say
std::vector<boost::shared_ptr<obj> > v;
//populate v
//now I want to clear v, I guess I have to do
for (int i = 0; i < v.size(); i++)
v.reset();


Again, not necessary - the whole point of smart pointers like
boost::shared_ptr is to do this work for you.
v.clear();

That takes care of it.

Best regards,

Tom


Thanks, Tom.
If I want to delete the vector, do I just say v.~vector(); without
specifying the deleting of its contents in the above case? But, If I
have vector<myobject*>, then I have to take care of each of them when
do clear and delete, right?
Have a good weekend.
zl2k
 
T

Thomas Tutone

zl2k said:
Thanks, Tom.
If I want to delete the vector, do I just say v.~vector(); without
specifying the deleting of its contents in the above case?

You are confusing "delete" with "destruct." You almost never would
explicitly call a vector's destructor, as you do above. The destructor
gets called automatically when the vector goes out of scope. If you
have a pointer to a vector that was created with new, you can delete
it, but you would be better off using a smart pointer to the vector
that took care of deleting it for you.
But, If I
have vector<myobject*>, then I have to take care of each of them when
do clear and delete, right?

Right. Which is why you are typically better off with a vector of
smart pointers, rather than a vector of raw pointers, since the former
will take care of themselves.

Best regards,

Tom
 
B

BobR

zl2k wrote in message
Thanks, Tom.
If I want to delete the vector, do I just say v.~vector(); without
specifying the deleting of its contents in the above case? But, If I
have vector<myobject*>, then I have to take care of each of them when
do clear and delete, right?
Have a good weekend.
zl2k

TT mentioned 'smart pointers'. Otherwise try this:

// ------------------------------------
template<class Seq> void PurgeCon( Seq &cont ){
for( typename Seq::iterator it( cont.begin() ); it != cont.end(); ++it){
delete *it;
*it = 0; // just in case it's called twice. (by mistake)
} // for(it)
} // template<class Seq> void PurgeCon(Seq&)
// ------------------------------------

// #include "myobject.h" // etc.

int main(){
// use 'scope' to control lifetime.
{ // scope
std::vector<myobject*> MyVec;
// fill and use 'MyVec'
// MyVec.push_back( new myobject( 12345 ) ); // etc.

PurgeCon( MyVec ); // you 'new', you 'delete'

MyVec.clear();
// fill and use 'MyVec' again
// PurgeCon( MyVec );
} // 'MyVec' is deleted at this point.

{ // scope
std::vector<int> MyVec; // yep! same name as above
// fill and use 'MyVec'
MyVec.push_back( 12345 );

// - NO!! - // PurgeCon( MyVec );
// 'MyVec' contains objects (which have destructors)

MyVec.clear(); // this line isn't needed, next line will do it.
} // 'MyVec' is deleted at this point.

return 0;
} // main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top