C++ STL 删除重复元素

180it 2020-10-13 AM 1664℃ 0条
#include<iostream>
#include<string>
#include<sstream>
#include<tuple>
#include<mutex>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;


struct OutPutItem {
    string match_vid;
    bool scmp(OutPutItem &l, OutPutItem &r) {
        return l.match_vid >= r.match_vid;
    }
};

int main(void)
{
    vector<OutPutItem> vct_output{ {"a"},{"b"},{"c"},{"a"},{"b"},{"c"} };
    //删除重复的元素
    sort(vct_output.begin(), vct_output.end(),[](OutPutItem &l, OutPutItem &r) {
        return l.match_vid < r.match_vid;
    });
    vector<OutPutItem>::iterator vector_iterator;
    vector_iterator = unique(vct_output.begin(), vct_output.end(), [](const OutPutItem &l, const OutPutItem &r) -> bool { return (l.match_vid == r.match_vid); });
    if (vector_iterator != vct_output.end())
    {
        vct_output.erase(vector_iterator, vct_output.end());
    }
    for (auto i : vct_output) {
        cout << i.match_vid << endl;
    }
    getchar();
    return 0;
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++ STL 删除重复元素