I'm trying to implement Vec::erase() custom function for learning purposes similar to the one which the original Vector class contains. That's what I've got so far:
template <class T> typename Vec<T>::iterator Vec<T>::erase(iterator pos)
{
if (pos == (avail - 1))
alloc.destroy(pos);
else
{
size_type new_size = avail - 1;
iterator new_data = alloc.allocate(new_size);
iterator temp_avail = uninitialized_copy(data, pos - ptrdiff_t(1), new_data);
iterator new_avail = uninitialized_copy(pos + ptrdiff_t(1), avail, temp_avail);
uncreate();
data = new_data;
avail = new_avail;
}
return avail;
}
My simplified version first checks if the element which is intended to be erased is the last one. In this case we just destroy the element and leave memory uninitialized. If the element is somewhere in the middle of the Vec, then we create a new Vec with the size =-1 , allocate memory for this size, copy elements from the previous Vec up to erased one, then append all remaining elements after the erased one. Then we destroy the original Vec with the function uncreate() and simply reassign data and avail to new values from the new Vec. May be the idea of the code is correct, but implementation is a total disaster. I have several errors with this code
1>source.cpp(131): error C2440: 'initializing': cannot convert from 'int *' to 'Vec::size_type' 1>source.cpp(131): note: There is no context in which this conversion is possible 1> source.cpp(126): note: while compiling class template member function 'int *Vec::erase(int *)' 1>source.cpp(156): note: see reference to function template instantiation 'int *Vec::erase(int *)' being compiled 1>source.cpp(149): note: see reference to class template instantiation 'Vec' being compiled
Please, help. Any advise would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire