思路
- 首先对 vector 中的元素使用 sort 函数排序,使重复的元素排在相邻的位置
- 接下来使用 unique 函数将重复的相邻元素放在 vector 末尾,返回值为末尾第一个重复元素的地址
- 最后调用 erase 函数,删掉重复元素
代码
#include <vector>
#include <algorithm>
using namespace std;
vector<TYPE> v;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
---------------------
原文:https://blog.youkuaiyun.com/sigmarising/article/details/80138869