implementing remove in std::allocator

S

ScOe

I've made a container class over std::allocator. Also implement create,
clear, push_back
but i need some assistance on removing item from container.

do i need to create empty container and than use push_back on all items
except item i want to delete or is there
any smoother way to do that ?

would this do the job ?
template <typename T> void TVecArray<T>::remove (iterator i)
{
alloc.destroy (i);
alloc.deallocate (i, 1);
}

implemented code (from accelerated c++):
std::allocator<T> alloc;
void push_back(const T& t){
if (avail == limit)
grow ();
unchecked_append (t);
}
template <typename T> void TVecArray<T>::grow ()
{
size_type new_size = max (2 * (limit - data), ptrdiff_t(1));
iterator new_data = alloc.allocate (new_size);
iterator new_avail = std::uninitialized_copy (data, avail, new_data);
uncreate ();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
template <typename T> void TVecArray<T>::unchecked_append (const T& val)
{
alloc.construct (avail++, val);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top