C++ Primer 第5版--练习10.11

来源:互联网 发布:单片机工程师笔试题 编辑:程序博客网 时间:2024/06/02 04:29

练习 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;}


0 0