C++ primer 第五版 中文版 练习 10.9
题目:实现你自己的elimDups。测试你的程序,分别在读取输入后、调用unique后以及调用erase后打印vector的内容。
答:
/*
实现你自己的elimDups。测试你的程序,分别在读取输入后、调用unique后以及调用erase后打印vector的内容。
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
cout << "vector用sort重排后的元素内容为:";
for (auto a : words)
cout << a << " ";
cout << endl;
auto end_unique = unique(words.begin(), words.end());
cout << "vector用unique重排后的元素内容为:";
for (auto a : words)
cout << a << " ";
cout << endl;
words.erase(end_unique, words.end());
cout << "vector中删除重复元素后的内容为:";
for (auto a : words)
cout << a << " ";
cout << endl;
}
int main()
{
vector<string> svect = { "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
cout << "vector中原始元素内容为:";
for (auto a : svect)
cout << a << " ";
cout << endl;
elimDups(svect);
return 0;
}