问题
读起来比较绕口,看下后面的效果图便于理解.
编写程序,使用stable_sort和isshorter[排序]将传递给你
的elim版本的vector排序.并打印vector.
——————————————
将vector中的元素按字典顺序重排,并消除重复单词.
//按长度重新排序,长度相同的单词维持字典顺序.
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /************************************************************************* > File Name: test.10.11.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年06月11日 ************************************************************************/ #include<iostream> #include<vector> #include<algorithm> using namespace std; /*************************** 编写程序,使用stable_sort和isshorter[排序]将传递给你 的elim版本的vector排序.并打印vector. ------------------------------------------ 将vector中的元素按字典顺序重排,并消除重复单词. //按长度重新排序,长度相同的单词维持字典顺序. ************************************/ //比较两个大小. bool isshort(const string &s1,const string &s2) { return s1.size()<s2.size(); } void out(vector<string> &ve) { cout << "********************" << endl; for(const auto &s:ve) { cout << s << endl; } cout << "********************" << endl; } //主要. void elim(vector<string> &vec) { cout << "未做任何操作之前:" << endl; out(vec); //排序并去掉重复的单词. sort(vec.begin(),vec.end()); auto re=unique(vec.begin(),vec.end()); cout << "排序后[已经将重复的单词移到尾部]:" << endl; out(vec); vec.erase(re,vec.end()); stable_sort(vec.begin(),vec.end(),isshort); cout << "最终结果:" << endl; out(vec); } int main() { cout << "无重复字符,按照字典顺序、字符长度顺序排序[输入单词]:" << endl; vector<string> vec; string pa; while(cin >> pa) { vec.push_back(pa); } elim(vec); return 0; } |