by CodeMage · 7y ago · 0 comments
The whole swap-and-pop section weirded me out. Maybe I just don't know enough about C++, but saying that assignment (a[i] = a[n-1]) will call the destructor seems false.
As far as I know, the compiler should generate an implicitly defined copy assignment operator for these fixed size PODs and it should be as performant as memcpy.
But again, I don't have years and years of in-depth C++ experience, so I would be grateful if an expert could shed more light on this.
I think that would just call the copy assignment operator, would it not?
For correctness you would probably then follow up with a pop_back to keep the vector right-sized.
Actually you'd probably want to do:
a[i] = std::move(a[n - 1]);
Then follow up with pop_back.
Best would probably be:
a[i] = std::move(a.erase(n-1));