Hello again, i just finished excersise 11-6 in Accelerated C++.
Through this chapter the book emulates a simplified vector class, and as a excersise i have written an erase() member function.
My solution allocates new memory, with the size of the original minus the size of what i erase. Then i copy everything but the part i wish to erase into the new storage. Finally i destroy and deallocate the old storage.
Is this the fastest way to erase?
The code has typedefs and functions but here it is anyway:
template <class T> void Vec<T>::erase(iterator& begin, iterator& end) {
if(begin) {
// New size = old size - (end - begin)
size_type new_size = (limit-data) - (end-begin);
// new storage
iterator new_data = alloc.allocate(new_size);
// copy first part into new storage
iterator temp = std::uninitialized_copy(data, begin-1, new_data);
// copy second part
iterator new_avail = std::uninitialized_copy(end+1, avail, temp);
// return the old space
uncreate();
// reset pointers to point to the newly allocated space
data = new_data;
avail = new_avail;
limit = new_data + new_size;
}
}