算法简述
- 有序向量去掉重复的元素
算法实现
// 有序向量的唯一算法, 返回删除的元素个数
int sortVectorUnique(vector<T> &ages) {
int old_size = ages.size();
int left_index = 0, right_index = 0;
while (++right_index < ages.size()) {
// 相邻不同的数值迁移到左值的下一个位置
if (ages[left_index] != ages[right_index]) {
ages[++left_index] = ages[right_index];
}
}
ages.resize(++left_index);
// TODO 缩容
return old_size - ages.size();
}