Your deletion code is invalid. The remove-erase idiom looks like this:
vector<int>::iterator it = remove(v.begin(), v.end(), 5);
v.erase(it, v.end());
In this case, it has the effect of erasing all values equal to 5, but it minimizes the number of copies needed to achieve this.
Your delete code only deletes the first value, which is 5, so it does what you want.
- , 5, ( std::remove), ( remove). undefined, 5 , remove v.end() .
, , 5, std::remove , 5 . 5 5 , 5s, std::partition std::remove:
auto it = partition(v.begin(), v.end(), [](int i) { return i != 5; });
if (it != v.end()) v.erase(it);
, 5 , , 5, , , :
auto it = partition(v.begin(), v.end(), [](int i) { return i != 5; });
if (it != v.end()) v.pop_back();
- , , 5 ( ), . it != v.end() -, , . v.erase(find(v.begin(), v.end(), 5)).