Learn a generic, endless!
STL's container is defined in STD NameSpace; so light is not desirable, at least:
Using namespace std; or: using std :: ××× (your container you use);
Behind the preparation instrument.
In fact, for GNU C , .c suffix and .CPP suffix have no separate, but for MS VS is the difference between C processes and C programs;
/ ************************************************** ***********************
In fact, the execution of the following programs written by Mr. Lippman is not as ideal as it is a book. Some problems on handling uppercase letters and symbols (his old man is probably deliberate). The main function of this program is to read String from two text files, each text file is placed in the String Vector, and the two string vectors are put in a String Vector, so generic algorithm processing Objects have, and then perform various generic operations. This program is stringing by a generic algorithm function object. Some things are really sorrowful: It is, the more I see the ultimate, it is a matter of course. Of course
It's time we should shoot, we want to modify this program so that this toy is improved. We imitate the practice of the sixth chapter to the String Vector advanced to the transformation of the letter, then, the punctuation in the erase, and finally perform a series of operations.
*********************************************************** ********************** /
#include
#include
#include
#include
/ / # include
#include
Using namespace std;
Class greaterthan {
PUBLIC:
Greaterthan (int size = 6): _size (size) {}
INT size () {return _size;}
BOOL Operator () (const string & s1) {
Return s1.size ()> 6;
}
Private:
INT _SIZE;
}
Template
Class Printelem {
PUBLIC:
Void Operator () (Const Type & Elem)
{
_ CNT;
IF (_CNT% 8 == 0) {cout << endl;}
COUT << ELEM << "
}
Private:
Static int _cnt;
}
Template
INT Printelem
Class lessthan {
PUBLIC:
Bool Operator () (Const String & S1, Const String & S2) {
Return S1.SIZE () } } Typedef Vector { IF (! pVEC) // Issue Warning Message Return; Vector Vector For (; it! = pvec-> end (); iTer) Copy (* ip) .begin (), (* iter) .end (), back_inserter (text)); // sort the elements of texts Sort (Texts.Begin (), Texts.end ()); For_each (Texts.begin (), Texts.end (), Printelem Cout << Endl << Endl; // delete all duplicate Elements Vector IT = unique (Texts.begin (), Texts.end ()); Texts.rase (IT, Texts.end ()); For_each (Texts.begin (), Texts.end (), Printelem Cout << Endl << Endl; Stable_sort (Texts.Begin (), Texts.end (), Lessthan ()); For_each (Texts.begin (), Texts.end (), Printelem Cout << Endl << Endl; // count Number of strings Greater Than Length 6 INT CNT = 0; // Obsolete form of count - Standard Changes this CNT = count_if (Texts.Begin (), Texts.end (), GreaterThan ()); COUT << "Number of Words Greater Than Length Six Are" << CNT << ENDL; // ... Static string rw [] = {"and", "ife", "or", "but", "the"}; Vector Vector For (; it2! = remove_words.end (); it2) { INT CNT = 0; // Obsolete form of count - Standard Changes this CNT = count (Texts.Begin (), Texts.end (), * IT2); Cout << CNT << "instances removed:" << (* IT2) << ENDL; TEXTS.ERASE Remove (Texts.Begin (), Texts.end (), * it2), Texts.end () ); } Cout << Endl << Endl; For_each (Texts.begin (), Texts.end (), Printelem } TypedEf Vector #include Main () { Vector Vector String T1FN, T2FN; COUT << "Text File # 1:"; CIN >> T1FN; COUT << "Text File # 2:"; CIN >> T2FN; IFStream Infile1 (t1fn.c_str ()); ifstream infile2 (t2fn.c_str ()); iStream_iterator iStream_iterator Copy (Input_Set1, EOS, Back_INSERTER (T1)); Copy (Input_Set2, EOS, Back_Inserter (T2)); Sample.push_back (t1); Sample.Push_Back (T2); Process_vocab (& Sample); } First we increase the function of filtering punctuation: Void filter_text (Vector { Const string filt_elex ("/" ,.;: !?) (///"); IF (filt_elex.empty ()) Return; Vector Vector While (iter! = iter_end) { String :: size_type pos = 0; While ((POS = (* iter) .find_first_of (filt_elems, pos)) ! = String :: Npos) (* iTer) .led (POS, 1); iTer; } } Secondly, add a function of converting uppercase: Void strip_caps (Vector { Const string Caps ("AbcdefghijklmnopqrStuvwxyz"); Vector Vector While (iter! = iter_end) { String :: size_type pos = 0; While (((POS = (* ip) .find_first_of (caps, pos)) ! = String :: Npos) (* iTer) [POS] = TOLOWER ((* iTer) [POS]); iTer; } } OK, insert these two functions in the appropriate location. The final program is as follows: #include #include #include #include #include #include Using namespace std; #include // PreStandard Syntax for // # include // Filter a function of punctuation Void filter_text (Vector { Const string filt_elex ("/" ,.;: !?) (///"); IF (filt_elex.empty ()) Return; Vector Vector While (iter! = iter_end) { String :: size_type pos = 0; While ((POS = (* iter) .find_first_of (filt_elems, pos)) ! = String :: Npos) (* iTer) .led (POS, 1); iTer; } } // Size write letter conversion function Void strip_caps (Vector { Const string Caps ("AbcdefghijklmnopqrStuvwxyz"); Vector Vector While (iter! = iter_end) { String :: size_type pos = 0; While (((POS = (* ip) .find_first_of (caps, pos)) ! = String :: Npos) (* iTer) [POS] = TOLOWER ((* iTer) [POS]); iTer; } } // The so-called function object is for functions so object Class greaterthan { PUBLIC: Greaterthan (int SZ = 6): _size (sz) {} INT size () {return _size;} Bool Operator () (Const String & S1) {Return S1.SIZE ()> _size; Private: INT _SIZE; } Class Printelem { PUBLIC: Printelem (int LineLen = 8) : _LINE_LENGTH (LINELEN), _CNT (0) {} Void Operator () (const string & elem) { _ CNT; IF (_CNT% _LINE_LENGTH == 0) { Cout << '/ n';} COUT << ELEM << " } Private: INT _LINE_LENGTH; INT_CNT; } // a Function Object - The Operation IS //Mplement as an instance of operator () Class lessthan { PUBLIC: Bool Operator () (Const String & S1, Const string & S2) { Return S1.SIZE () } } // The following code has been commented Because 'Allocator' // Being a Template Class, Should Be Given Template Arguments. // Typedef Vector Typedef Vector // The following code has been commented Because 'Allocator' // Being a Template Class, Should Be Given Template Arguments. // void process_vocab (Vector Void Process_vocab (Vector { IF (! pVEC) { // Test Pointer to Vector // Issue Warning Message COUT << "Invalid Argument ..... / n"; Return; } // The following code has been commented Because 'Allocator' // Being a Template Class, Should Be Given Template Arguments. // vector Vector // The following code has been commented Because 'Allocator' // Being a Template Class, Should Be Given Template Arguments. // vector Vector For (ney = pvec-> begin (); it! = pvec-> end (); iTer) COPY (* ip) .begin (), (* iter) .end (), Back_INSERTER (Texts)); // Before Operate Cout << "/ nbefor Operating 'Texts' Contains: / N"; For_each (Texts.Begin (), Texts.end (), Printelem ()); Cout << "/ n / n"; Filter_Text (Texts); Strip_caps (Texts); // sort the elements of texts Sort (Texts.Begin (), Texts.end ()); // OK, Let's See What We Have Cout << "/ nafter Applying 'sort ()' The Vector 'Texts' Contains: / N"; For_each (Texts.Begin (), Texts.end (), Printelem ()); COUT << "/ n / n"; // Just to Separate Display Output // delete all duplicate Elements // Vector Vector IT = unique (Texts.begin (), Texts.end ()); // ok, let's see what we have now Cout << "/ nafter Applying 'unique ()' The Vector 'Texts' << "Contains: / N"; For_each (Texts.Begin (), Texts.end (), Printelem ()); Cout << "/ n / n"; Texts.rase (IT, Texts.end ()); // ok, let's see what we have now Cout << "/ nafter Applying 'Erase ()' The Vector 'Texts' << "Contains: / N"; For_each (Texts.Begin (), Texts.end (), Printelem ()); Cout << "/ n / n"; // sort the elements based on Default Length of 6 // stable_sort () preserve 4 ORDERING OF Equal Elements ... Stable_sort (Texts.Begin (), Texts.end (), Lessthan ()); Cout << "/ NAFTER Applying 'Stable_Sort ()' The Vector 'Texts' Contains: / N"; For_each (Texts.Begin (), Texts.end (), Printelem ()); Cout << "/ n / n"; // count Number of strings Greater Than Length 6 INT CNT = 0; // Obsolete form of count - Standard Changes this CNT = count_if (Texts.Begin (), Texts.end (), GreaterThan ()); COUT << "Number of Words Greater Than Length Six Are" << CNT << ENDL; Static string rw [] = {"and", "if", "or", "but", "the"}; // vector Vector // Vector Vector REMOVE_WORDS.BEGIN (); For (; it2! = remove_words.end (); IT2) { INT CNT = 0; // unobsolete form of count - Standard Changes this CNT = count (Texts.Begin (), Texts.end (), * IT2); Cout << CNT << "instances removed:" << (* IT2) << ENDL; TEXTS.ERASE Remove (Texts.Begin (), Texts.end (), * IT2), Texts.end () ); } Cout << "/ n / n"; Cout << "/ NAFTER Applying 'Erase ()' The Vector 'Texts' Contains: / N"; For_each (Texts.Begin (), Texts.end (), Printelem ()); } // Difference Type is The Type Capable of Holding The Result // of subtracting two iptracties of a container // - in this case, of a string vector ... // Ordinarily, this is handled by Default ... // The Following Code Has Been Comment Because 'Allocator' Being A // Template Class, SHOULD BE GIVEN TEMPLATE ARGUMENTS. // typedef vector TypedEf Vector // PreStandard header Syntax for #include int main () { Vector // The Following Code Has Been Comment Because 'Allocator' Being A // Template Class, SHOULD BE GIVEN TEMPLATE ARGUMENTS. // vector Vector String T1FN, T2FN; // Request Input Files from User ... // Should Do Some Error Checking in real-world programcout << "Text File # 1 (Infile.txt):"; CIN >> T1FN; COUT << "Text File # 2 (Infile1.txt):"; CIN >> T2FN; // Open the files IFStream Infile1 (t1fn.c_str ()); ifstream infile2 (t2fn.c_str ()); // Special Form of Iterator // Ordinarily, DIFF_TYPE IS Provided by DEFAULT ... iStream_iterator EOS; iStream_iterator // Special Form of Iterator Copy (Input_Set1, EOS, Back_INSERTER (T1)); Copy (Input_Set2, EOS, Back_Inserter (T2)); Sample.push_back (t1); Sample.Push_Back (T2); Process_vocab (& Sample); Return 0; } The results of the execution are as follows: Text File # 1 (Infile.txt): Infile.txt TEXT FILE # 2 (Infile1.txt): Infile1.txt BEFOR OPERATING 'TEXTS' Contains: Alice Emma Has Long Flowing Red Hair. Her Daddy Says When The Wind Blows THROUGH Her Hair, IT Looks Almost Alive, Like A Fiery Bird in flight. "a beautiful fiery bird", HE Tells Her, "Magical Butned." "Daddy, Shush, There is no self shing, "She Tells Him, At the Same Time Wanting Him To Tell Her morely, she asks, "i mean, daddy, Is there? "Magical Butned." Daddy, Shush, There Is no self shing, " After Applying 'Sort ()' The Vector 'Texts' Contains: A A Alice Alive Almost Asks At Beautiful Bird Brows But But Daddy Daddy Daddy Daddy Emma Fiery Fiery Flight Flowing Hair Hair Has He Her Her Her Her Her Him I I I I is IT Like Long Looks Magical Magical Mean More No NO Red Same Says She SHE SHUSH SHUSH SHYLY Such Such Tell Tells Tells Tells the the the the THERE THING THROUGH TIME TOETAMED Untamed Wanting When WinDafter Applying 'Unique ()' The Vector 'Texts' Contains: A Alice Alive Almost Asks At Beautiful Bird Blows But Daddy Emma Fiery Flight Flowing Hair Has He Her Him I in IS IT Like Long Looks Magical Mean More No Red Same Says She Shush Shyly Such Tell TELLS the the the the the the turn through time to us Wanting when WIND SHE SHUSH SHUSH SHYLY Such Such Tell Tells Tells Tells the the the the THERE THING THROUGH TIME TOETAMED Untamed Wanting When Wind After Applying 'Erase ()' The Vector 'Texts' Contains: A Alice Alive Almost Asks At Beautiful Bird Blows But Daddy Emma Fiery Flight Flowing Hair Has He Her Him I in IS IT Like Long Looks Magical Mean More No Red Same Says She Shush Shyly Such Tell TELLS the the the the the the turn through time to us Wanting when wind After Applying 'Stable_sort ()' The Vector 'Texts' Contains: A I at He In is IT No to But Has Her Him Red She The asks Bird Emma Hair Like Long Mean More Same Says Such Tell Time When Wind Alice Alive Blows Daddy Fiery Looks Shush Shyly TELLS There Thing Almost Flight Flowing Magical THROUGH Untamed Wanting Beautiful Number of Words Greater Than Length Six Are 6 0 Instances Removed: and 0 Instances Removed: IF 0 Instances Removed: OR 1 INSTANCES Removed: BUT 1 INSTANCES REMOVED: THE After Applying 'Erase ()' The Vector 'Texts' Contains: A I at He In is IT NO to Has Her Him Red She Asks Bird Emma Hair Like Long Mean More Same Says Such Tell Time When Wind Alice Alive Blows Daddy Fiery Looks Shush Shyly Tells Twhere Thing Almost Flight Flowing Magical THROUGH Untamed Wanting Beautiful The Infile file is as follows: Alice Emma Has Long Flowing Red Hair. Her Daddy Says When Thewind Blows Through Her Hair, IT Looks Almost Alive, Like A Fiery Bird in flight. "a beauty fiery bird", He Tells Her, "Magical But Untamed. " Daddy, Shush, There Is No Such Thing, "She Tells Him, AT The Same Time Wanting Him To Tell Her More. Shyly, She Asks, "I mean, daddy, is there?" The Infile1 file is as follows: "Daddy, Shush," "Daddy, Shush,"