question related to stl vector and boost::shared_ptr



 DEVELOP > c-Plus-Plus > question related to stl vector and boost::shared_ptr

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "zl2k"
Date: 22 Oct 2006 11:49:32 AM
Object: question related to stl vector and boost::shared_ptr
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[i].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[i].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
.

User: "Thomas Tutone"

Title: Re: question related to stl vector and boost::shared_ptr 22 Oct 2006 12:16:41 PM
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[i].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[i].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
.
User: "zl2k"

Title: Re: question related to stl vector and boost::shared_ptr 22 Oct 2006 12:28:26 PM
Thomas Tutone wrote:

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[i].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[i].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
.
User: "BobR"

Title: Re: question related to stl vector and boost::shared_ptr 22 Oct 2006 06:02:59 PM
zl2k wrote in message
<1161538106.341153.105790@m7g2000cwm.googlegroups.com>...


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()
--
Bob R
POVrookie
.

User: "Thomas Tutone"

Title: Re: question related to stl vector and boost::shared_ptr 22 Oct 2006 04:30:11 PM
zl2k wrote:

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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER