4 ms·
Ideally, the std lib implementation should handle that detail for you...
by hermitdev 7y ago
Ideally, the std lib implementation should handle that detail for you...
- gpderetta 7y agowhich detail?
- foota 7y agoIn theory erase could return a move iterator, meaning that you could omit the call to std::move. This wouldn't be backwards compatible though so not going to happen.
- gpderetta 7y agowait, 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.