template <typename T>
int eliminate_duplicate(vector<T> &A) {
sort(A.begin(), A.end()); // makes identical elements become neighbors
auto it = unique(A.begin(), A.end()); // removes neighboring duplicates
A.resize(it - A.cbegin()); // truncates the unnecessary trailing part
return it - A.cbegin(); // Question> Is this line valid?
}
Is the last line valid?
Here is my concern: after the calling of resize
, the iterator it
will point to a invalidate location, so can we still use it as it - A.cbegin()
in the return line?