练习 10.11:编写程序,使用 stable_sort 和 isShorter 将传递给你的 elimDups版本的 vector 排序。打印 vector 的内容,验证你的程序的正确性。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
elimDups(words);
stable_sort(words.begin(), words.end(), isShorter);
for (const auto &s : words)
cout << s << " ";
cout << endl;
return 0;
}