思路
- 首先對 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.csdn.net/sigmarising/article/details/80138869