那么你可以用std :: vector做到这一点.
在这两种情况下,erase都将迭代器作为参数.
因此,在您从矢量(或ptr_vector)中删除某些内容之前,您需要找到它.
另请注意,ptr_vector将其内容视为已存储对象而非存储指针.所以任何搜索都是通过对象完成的.
所以基本上
std::vector x; std::ptr_vector y; // These two object should behave in exactly the same way. // The ONLY difference is inserting values which for y are pointers. // Y take ownership of the pointer and all subsequent acesses to the // members of y look like they are objects
例:
#include#include class A { int m; public: A(int x):m(x) {} bool operator==(A const& rhs) {return m = rhs.m;} }; int main() { boost::ptr_vector x; x.push_back(new A(1)); x.erase(std::find(x.begin(),x.end(),A(1))); std::vector y; y.push_back(A(2)); y.erase(std::find(y.begin(),y.end(),A(2))); // To find an exact pointer don't modify the equality. // Use find_if and pass a predicate that tests for a pointer A* a = new A(3); boost:ptr_Vector z; z.push_back(a); z.erase(std::find_if(y.begin(),y.end(),CheckPointerValue(a)); } struct CheckPointerValue { CheckPointerValue(A* a):anA(a) {} bool operator()(A const& x) { return &X == anA;} private: A* anA; };