删除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页

浙公网安备 33010602011771号