首页 > 编程 > C++ > 正文

C++ 字符串序列 进行长度 排序的同时,保持字典序排序

2019-11-06 06:50:42
字体:
来源:转载
供稿:网友

elimDups实现了字典排序: unique()算法的作用是吧重复的元素放到最后并用”???”覆盖;然后用容器erase操作来删除后面。 stable_sort()就是在长度排序下,保持字典序。

#include <iostream>#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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选