In my example auto can be replaced by the iterator type. If it's an vector of int it could be written as
1 2 3 4 5 6
|
std::vector<int>::iterator it = std::find(v.begin(), v.end(), value);
if (it != v.end())
{
*it = v.back();
v.pop_back();
}
|
std::move was used because some objects are more efficient to move than to copy. For simple types like int there is no difference. Moving is a C++11 feature and you don't need to use it. If you can't use std::move but still want to avoid the costly copying you could use std::swap. Using std::swap will be slower for simple types like int but for some types like std::string, std::vector, etc. it is often more efficent because it avoids the need to have to copy all the elements stored in the string/vector.
If you are going to use std::remove note that it will have to move all elements after a removed element (it doesn't use the pop_back() trick) so it might be slower. Also note that std::remove will remove all elements that match, so in Catfish's example, if there was 5 elements with the value 666 it would remove all 5 elements. My example will just remove the first if there is more than one match.
std::remove doesn't really remove any objects from the container. It just moves the objects and returns an iterator to the new end, leaving garbage at the end. Catfish's code assumes there there is exactly one element removed, and his edit assumes no more than one element was removed. A better way to use std::remove is to pass the return value to erase like this:
vi.erase(std::remove(vi.begin(), vi.end(), 666), vi.end());
No need to use if statements or anything. This will always work.