删除vector中的重复项

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
void elimDups(std::vector<std::string>& words)
{
	std::sort(words.begin(), words.end());
	auto end_uniques = std::unique(words.begin(), words.end());
	words.erase(end_uniques, words.end());
}

int main()
{
	std::vector<std::string> words = { "1","2","3","1","2" };
	elimDups(words);
	for (auto word : words)
	{
		std::cout << word << "\n";
	}

	return 0;
}

代码如上

参考:

C++ primer中文版(第5版) 343页

posted @ 2023-08-10 11:21  强里秋千墙外道  阅读(55)  评论(0)    收藏  举报  来源