c++ vector删除重复元素的2种方法

#include <iostream>
#include <vector>
#include<algorithm>
#include<cassert>

using namespace std;

int main()
{
    
    
    vector<int> a = {
    
    2, 1, 5, 2, 3, 4, 1, 2, 2, 3};
    sort(a.begin(), a.end());

    //a.erase(unique(a.begin(), a.end()), a.end());
    a.resize(distance(a.begin(), unique(a.begin(), a.end())));

    return 0;
}

猜你喜欢

转载自blog.csdn.net/bigfanofloT/article/details/111383559