vector::erase() also erases member-vectors of a st

2019-05-26 20:15发布

问题:

People, I am new to all this programming talk. Up until now it was quite easy to find answers by googling them but right here I have big trouble expressing what I want to ask, let me try: Erasing a vector calls the destructor before freeing the memory, right? Now how does a struct-vector react, if it is destructed? One does not define a destructor for these things, but is it correct to assume that if a struct is "destructed" each of its members' destructors will be called as well?

Let me give you an example:

#include <string>
#include <vector>
struct ding_t {
    std::string dang;
} foo;

strung boom_t {
    vector <ding_t> chuck;
} bar;

int main () {
    vector <boom_t> tom;
    tom.resize(10);
    tom[4].chuck.resize(5);
    tom[4].chuck[3].dang = "jerry";
    tom.erase();

    return 0;
}

in this case, will the memory allocated by

tom[4].chuck.resize(5);

be freed as well? Sorry for my vocabulary, but at this moment I am trying to move from pointers to the more sophisticated cpp language equivalent of vectors. I hope I got my point across. Thanks in advance guys and please just redirect me if this has already been asked, as I've said, I don't know how to circumscribe this question.

回答1:

Yes, the memory will be freed automatically.

When a vector is destructed it will call the destructor of all the elements it contains. You didn't define a destructor for your struct so the compiler will provide a default one for you (that does nothing).

However if your vector contains pointers to objects it will be your responsibility to call the destructor on the objects before destructing the vector (because the vector will call the destructor of the pointers, not the pointed objects), if you have no other way to access them later.



回答2:

See http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.11 for the answer to your question and the entire article for a very good treatment of destructors in C++.

As to your second question: yes, the memory allocated by tom[4].chuck.resize(5); will get freed as well, since it is the vector's responsibility to manage its own memory allocations (which is the case for the "resize()" call.)



回答3:

Answer: Since you are not allocating the Object dynamically with the new operator, you dont have to deallocate them manually. It's done automatically for you.

Ok. Back to your code :)

If you want to erase the 6th element then use tom.erase (tom.begin()+5) .

And if you want to erase all elements then use tom.erase (tom.begin(),tom.end() ) .

To erase the first 3 elements use tom.erase (tom.begin(),tom.begin()+3).

#include <string>
#include <vector>
using namespace std;

struct ding_t 
{
    std::string dang;
} foo;

struct boom_t {
    std::vector <ding_t> chuck;
} bar;

int main () {
    vector <boom_t> tom;
    tom.resize(10);
    tom[4].chuck.resize(5);
    tom[4].chuck[3].dang = "jerry";

    //error C2661: 'erase' : no overloaded function takes 0 parameters
    //tom.erase( );

    // erase the 6th element
  tom.erase (tom.begin()+5);

    // erase the first 3 elements:
  //tom.erase (tom.begin(),tom.begin()+3);

    // erase everything:
  //tom.erase (tom.begin(),tom.end() );

    return 0;
}


回答4:

Okay, I've done this little check, just to make sure. (Why didn't I think of this earlier... was quite late yesterday... ) The initial code was badly written and didn't work, apologies for that.

This:

#include <string>
#include <vector>

struct ding_t {
    std::string dang;
} foo;

struct boom_t {
    std::vector <ding_t> chuck;
} bar;

int main () {
    std::vector <boom_t> tom;

    while (true) {
        tom.resize(10);
        tom[4].chuck.resize(5);
        tom[4].chuck[3].dang = "jerry";
        tom.erase( tom.begin(), tom.end() );
    }

    return 0;
}

causes no memory leak, the used memory is stable.