I want to clear ptr_array
boost::ptr_array<A, 10> a;
...
a.clear(); // missing
How do I clean ptr c开发者_运维技巧ontainer?
It is supposed to behave like an array and you can not clear an array in C++
. Only thing you can do is to set the individual elements to NULL.
According to the class synopsis, calling a.release();
will do the trick, as the docs state that a postcondition of calling release
is that "all pointers are null."
Indeed, a glance at the implementation verifies this, although it's less efficient than strictly possible since it involves an unused/wasted (for your purposes) heap allocation:
std::auto_ptr<this_type> release()
{
std::auto_ptr<this_type> ptr( new this_type );
this->swap( *ptr );
return ptr;
}
精彩评论