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);
}
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);
}