views:

109

answers:

2

hi,

I have a vec_A that stores instances of class A as: vec_A.push_back(A());

I want to remove some elements in the vector at a later stage and have two questions: a) The element is deleted as: vec_A.erase(iterator) Is there any additional code I need to add to make sure that there is no memory leak? .

b) Assume that condition if(num <5) is if num is among a specific numberList. Given this, is there a better way to delete the elements of a vector than what I am illustrating below?

#include<vector>
#include<stdio.h>
#include<iostream>

class A {
      public:

             int getNumber();            
             A(int val);
             ~A(){};
      private:
              int num;
};

A::A(int val){
         num = val;
         };

int A::getNumber(){
    return num;
};

int main(){


    int i  =0;
    int num;
    std::vector<A> vec_A;
    std::vector<A>::iterator iter;

          for ( i = 0; i < 10; i++){
              vec_A.push_back(A(i));
          }
          iter = vec_A.begin();

          while(iter != vec_A.end()){
              std::cout <<  "\n --------------------------";
              std::cout <<  "\n Size before erase =" << vec_A.size();
              num = iter->getNumber() ;
              std::cout <<  "\n num = "<<num;
              if (num < 5){
                      vec_A.erase(iter);
                      }
              else{
                   iter++;
                   }

              std::cout <<  "\n size after erase =" << vec_A.size();
          }        


    std::cout << "\nPress RETURN to continue...";
    std::cin.get();

    return 0;
}
+4  A: 

a) The element is deleted as: vec_A.erase(iterator) Is there any additional code I need to add to make sure that there is no memory leak? .

Yes, that's all you need to do. There will be no memory leak. Since you didn't allocate yoour object on heap, when you did vec_A.push_back(A()) a new object is copied into the vector. When you do erase, vector will take care of deleting the elements.

Assume that condition if(num <5) is if num is among a specific numberList. Given this, is there a better way to delete the elements of a vector than what I am illustrating below?

Yes, you can remove/erase idiom. This is an example:

 class A
{
public:
    A(int n) : m_n(n)
    {
    }

    int get() const
    {
        return m_n;
    }
private:
    int m_n;
};

bool lessThan9(const A& a)
{
    return a.get() < 9;
}

//Or if you want for a generic number
struct Remover : public std::binary_function<A,int,bool>
{
public:
    bool operator()(const A& a, int n)const
    {
        return a.get() < n;
    }
};

int main()
{
    std::vector<A> a;
    a.push_back(A(10));
    a.push_back(A(8));
    a.push_back(A(11));
    a.push_back(A(3));

    a.erase(std::remove_if(a.begin(), a.end(), lessThan9), a.end());

    //Using the user-defined functor
    a.erase(std::remove_if(a.begin(), a.end(), std::bind2nd(Remover(), 9)), a.end());

    return 0;
}
Naveen
Why not just `a.erase(std::remove_if(a.begin(), a.end(), lessThan9), a.end());`
Andreas Brinck
Thanks Naveen! This is very useful. .Also, thanks to Andreas for the additional comment I think Naveen has updated his code
memC
+1  A: 

1) Resource handling is done by the class itself. The destructor of a class is responsible to ensure that there is no memory leak.

2) Removing elements from a vector is best done back-to-front:

for (std::vector<A>::reverse_iterator it = vec_A.rend(); it != vec_A.rbegin(); --it)
{
  if (it->getNumber() < 5) {vec_A.erase(it.base());}
}
Michael Ulm
this (earasing from back to front) is useful to know, although I don't always have condition if ( num <5) .
memC
I don't see why you think removing back-to-front is significantly better. It's best done with the "remove-erase" idiom (which makes the whole operation *single-pass*).
UncleBens
The disadvantage of the remove-erase idiom is the need for a predicate. I don't like having to write a function lessThan5 just for one erase. Having said that, remove-erase is my idiom of choice if the predicate already exists or can be reused (or if I have lambdas).
Michael Ulm