1. 程式人生 > >C++ STL 刪除重複元素

C++ STL 刪除重複元素

#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;
}