#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页
本文介绍了如何使用C++的sort和unique函数从vector中移除重复的std::string元素。通过实例展示了elimDups函数的工作原理,并在main函数中演示了去重后的输出。
787

被折叠的 条评论
为什么被折叠?



