Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question related to stl vector and boost::shared_ptr

Reply
Thread Tools

question related to stl vector and boost::shared_ptr

 
 
zl2k
Guest
Posts: n/a
 
      10-22-2006
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

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      10-22-2006
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

 
Reply With Quote
 
 
 
 
zl2k
Guest
Posts: n/a
 
      10-22-2006

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

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      10-22-2006
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

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-22-2006

zl2k wrote in message
<. 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57