使用擦除删除 std 向量的元素对象:a) 内存处理和 b) 更好的方法? [英] deleting element objects of a std vector using erase : a) memory handling and b) better way?
问题描述
我有一个 vec_A
将 class A
的实例存储为:vec_A.push_back(A());
I have a vec_A
that stores instances of class A
as: vec_A.push_back(A());
我想在后期删除向量中的一些元素,有两个问题:a) 元素被删除为:vec_A.erase(iterator)
是否有任何额外的代码我需要添加以确保没有内存泄漏?.
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) 假设条件 if(num <5)
是如果 num 在特定的 numberList 中.鉴于此,是否有比我在下面说明的更好的方法来删除向量的元素?
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;
}
推荐答案
a) 该元素被删除为:vec_A.erase(iterator) 有没有我需要添加的附加代码确定没有内存泄漏?.
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? .
是的,这就是你需要做的.不会有内存泄漏.由于您没有在堆上分配对象,因此当您执行 vec_A.push_back(A()) 时,一个新对象被复制到向量中.当你擦除时,vector 会负责删除元素.
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.
假设条件 if(num <5) 是 ifnum 在一个特定的 numberList 之中.鉴于此,有没有更好的方法来删除向量的元素比我在下面说明了什么?
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;
}
这篇关于使用擦除删除 std 向量的元素对象:a) 内存处理和 b) 更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!