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;}新闻热点
疑难解答
图片精选