3 ms·
wait, how is this supposed to work? a[i] = std::move(a.erase(n-1)); There is no erase that takes an index, so I assume that n = a.end(). Also it is missing
by gpderetta 7y ago
wait, how is this supposed to work?
a[i] = std::move(a.erase(n-1));
There is no erase that takes an index, so I assume that n = a.end(). Also it is missing a dereference:
a[i] = std::move(*a.erase(a.end()-1));
but erasing the one-before-the-end returns the (new) end iterator, which obviously is not referenceable. In general, after calling erase, it is too late to access the erased element.
You want something like:
template<class Container, class Iter>
auto erase_and_return(Container&& c, Iter pos)
{
auto x = std::move(*pos);
c.erase(pos);
return x;
}
Also in the general case it doesn't make sense for erase to return a move iterator.
- foota 7y agoThanks for the corrections. I mis-read the documentation and thought erase returned an iterator to the elements erased.