标签:algorithm mil ios ++ bool 练习 back pre png
练习10.11
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 #include<fstream> 6 using namespace std; 7 8 void elimDups(vector<string> &words); 9 bool isShorter(const string &s1, const string &s2); 10 11 int main() 12 { 13 ifstream in("test.txt"); 14 if (!in) 15 { 16 cout << "无法打开文件" << endl; 17 exit(1); 18 } 19 20 vector<string> vec; 21 string word; 22 while (in >> word) 23 vec.push_back(word); 24 cout << "排序前:"; 25 for (auto &s : vec) 26 cout << s << " "; 27 cout << endl; 28 elimDups(vec); 29 stable_sort(vec.begin(), vec.end(), isShorter); 30 cout << "排序后: "; 31 for (auto &s : vec) 32 cout << s << " "; 33 return 0; 34 } 35 36 bool isShorter(const string &s1, const string &s2) 37 { 38 return s1.size() < s2.size(); 39 } 40 41 void elimDups(vector<string> &words) 42 { 43 sort(words.begin(), words.end()); 44 auto unique_end = unique(words.begin(), words.end()); 45 words.erase(unique_end, words.end()); 46 }
【运行结果】
标签:algorithm mil ios ++ bool 练习 back pre png
原文地址:https://www.cnblogs.com/sunbines/p/8809876.html