Passing functions to an algorithm
Exercise 10.11
1#include <iostream>2#include <string>3#include <vector>4#include <algorithm>5#include <fstream>6 using namespacestd;7 8 voidElimdups (vector<string> &words);9 BOOLIsshorter (Const string&S1,Const string&S2);Ten One intMain () A { -Ifstreaminch("Test.txt"); - if(!inch) the { -cout <<"Unable to open file"<<Endl; -Exit1); - } + -vector<string>Vec; + stringWord; A while(inch>>word) at Vec.push_back (word); -cout <<"before sorting:"; - for(Auto &S:vec) -cout << S <<" "; -cout <<Endl; - elimdups (VEC); in Stable_sort (Vec.begin (), Vec.end (), isshorter); -cout <<"after sorting:"; to for(Auto &S:vec) +cout << S <<" "; - return 0; the } * $ BOOLIsshorter (Const string&S1,Const string&S2)Panax Notoginseng { - returnS1.size () <s2.size (); the } + A voidElimdups (vector<string> &words) the { + sort (Words.begin (), Words.end ()); -Auto Unique_end =Unique (Words.begin (), Words.end ()); $ words.erase (Unique_end, Words.end ()); $}
" Run Results "
"C + + Primer" Tenth generic Algorithm (i)