question related to stl vector and boost::shared_ptr

Discussion in 'C++' started by zl2k, Oct 22, 2006.

  1. zl2k

    zl2k Guest

    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
    zl2k, Oct 22, 2006
    #1
    1. Advertising

  2. 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
    Thomas Tutone, Oct 22, 2006
    #2
    1. Advertising

  3. zl2k

    zl2k Guest

    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.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
    zl2k, Oct 22, 2006
    #3
  4. 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
    Thomas Tutone, Oct 22, 2006
    #4
  5. zl2k

    BobR Guest

    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()

    --
    Bob R
    POVrookie
    BobR, Oct 23, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Mastro

    boost::shared_ptr inside stl::list

    James Mastro, Nov 13, 2003, in forum: C++
    Replies:
    1
    Views:
    2,022
    Cy Edmunds
    Nov 13, 2003
  2. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,726
    Kai-Uwe Bux
    Jun 2, 2006
  4. zl2k
    Replies:
    3
    Views:
    1,004
  5. Colin Caughie
    Replies:
    1
    Views:
    696
    Shooting
    Aug 29, 2006
Loading...

Share This Page