"C ++ Primer" learning notes scattered ... (Not finished ...)

xiaoxiao2021-03-06  85

In front: C primer is not suitable for "beginners", beginners still watch "C Primer Plus" I have not seen it, but the road to say that the hard work is good, learning C is really difficult! In fact, I have been looking back, I'm going back, I will repeat, because my heart is inert, it is not bad, in fact, if you think it is a matter of course, you may not know very clearly. So I am determined to see it again from my head. I want to implement the procedures and exercises from the head to the end, and write the experience and notes, the reason why this is for myself, one is for learning C primer and very painful people, I have heard a Ph.D. telling the C course, I feel that it is a waste of time, because according to my experience, the last part of each chapter of the textbook is the most useful. Part of the school is slightly over school because the exam will not test. Because it is self-study, there is often no one to answer when you encounter problems. The C Primer learning note on the Internet is too simple. There is not much value, before the fifth chapter, I have never encountered too much questions, I Skating the second chapter, after learning the chapter of the following, is qualified to see the second chapter. In order to better analyze C primer, I will look at the following books in my hand: "The C Programming Language" by bjarne stroustrup "The C Standard Library" by nicolai m.josuttis " Effective C "by Mayer" More Effective C "BY Mayer" C STL Chinese version "BY PJPLAGER" Master the standard C class "by Cameron Hughes, Tracey Hughes" STL for C programming "by David etc" Think In C "by bra" C University Tutorial "BY HM, PD ... ...... Chapter 5: Compilation of the ILIST class, change. C is .cpp; first, should put # Include "iostream.h" all modulate "#include" iostream ", the same fstream.h -> fstream; stddef.h -> cstddef, you may have to attach the corresponding file header for other programs, this is to see what the program is used Features; Second, increase the use of Namespace STD to the pre-processor; third, remove allocator fourth, g -o ilist.exe ilist.cpp ilistmain.cpp; success! 5.11 p202, about _current _current is pointing one IList_item pointer, meaning is always pointing to the IList_item item of current activity, if deleting an ilist's head, _current still pointing to the chain header is not a wonderful, so it is impossible to modify Remove and REMOVE_FRONT.

Chapter 6 MAIN0.CPP Program Note // Increase #include Go to Program Head // Enter: G -o Main0.exe Main0.cpp # include #include #include #include #include #include #include // Because this is a must, the original program does not contain #include #include Using namespace std; // For convenience, it is explicitly specified name space is a standard name space extern vector * retrieve_text (); // This sentence is that Retrieve_Text is already defined somewhere, let the compiler yourself Looking, Main will use int main () {vector * text_file = Retrieve_text (); // text_file means a pointer to a vector consisting of String, returned by Retrieve_Text << "------- ---- About to generate text read ------------ / n "; ostream_iterator Output (cout," / n "); // This sentence is Output is a Output flow, this iterator locks the String type, binds to the COUT standard output object, most of which is displayed, insert / n wraps Copy for each line read (TEXT_FILE-> Begin (), text_file -> end (), output); // Copy all text_file to the standard output, in other words, insert the text_file into the standard output

Return 0;} // I first narrodely what the retrieve_text function is doing and how to do it, this function reads each line in the input text file, record its contents, why do you think, you think about it? Look - it reads the entered text file, looks, seeing String, put it in the vector in the vector, in the vote of the vector is the first line of the text file, and push it. How is it doing? First, it builds an IFStream object. This object represents yourself on behalf of you, then getting GetLine, pay attention to getLine is a Getline provided for stream, one is Getline provided for String in , here It is the latter. GetLine as the name, it is a line of the file, in fact, it is not necessarily, depending on the third parameter of GetLine, the default parameter is "/ n", "/ n" means getline is a veritable Get line, next vector PUSH_BACK Dinner debut, it uses each line obtained from getLine as a String into the VECTOR container, and the size of the Vector will be automatically added. Other parts are very simple, this is slightly.

Vector * retrieve_text () {string file_name; cout << "please enter file name:"; cin >> file_name; ifstream infile (file_name.c_str (), ios :: in); // infile is an IFStream / / Object, of course, ifstream is a template class defined by typedef. It locks the char type as a parameter. The constructor requires a const char *, iOS :: in meaning is that iOS is a stream template that locks the char type, and IN is telling the stream to open a file if (! infile) {cerr << "OOPS! Unable to open file" << file_name << "- bailing out! / n"; exit (-1); Else if (Infile.eof ()) {// I have a modification, if the text file is empty, then exit does not do such a handle will crash CERR << "in this case, Cerr <<" please don't input A Empty file "; cout <<" / n "; exit (-1);} vector * lines_of_text = new vector ; string textline; typedef pair stats; stats maxline; INT LINENUM = 0; While (getLine (infile, textline, '/ n')) {cout << "line read:" << TextLine << "/ n"; if (Maxline.First push_back (textline); LINENUM ;} Cout << "/ n"; cout << "Number of lines:" << Lines_of_text-> size () << "/ n"; cout << "Maximum Length:" << maxline.first << "/ n" COUT << "LONGEST line:" * Lines_of_text) [Maxline.Second] << "/ n"; return lines_of_text;} / * dumbo [107] ~ / d.stdlib =>

a.outplease enter file name: alice_emma line read: Alice Emma has long flowing red hair Her Daddy saysline read:. when the wind blows through her hair, it looks almost alive, line read:. like a fiery bird in flight A beautiful fiery Bird, HE Tells Her, Line Read: Magical Butne, SHING, "LINE Read: She Tells Him, At the Same Time Wanting Him To Tell Her More.Line Read: Shyly, She Asks , "I mean, daddy, is there" Number of Lines: 6Maximum Length: 66longest Line: Like a Fiery Bird in flight. A Beautiful Fiery Bird, He Tells her, main1.cpp program Note #include #include #include #include #include #include #include #include using namespace std; typedef pair location; typedef vector Loc; typef vector text; typef pair text_loc; extern vector * retrieve_text (); extern text_loc * Separate_words (const vector *); e xtern void display_text_locations (text_loc *); int main () {vector * text_file = retrieve_text (); text_loc * text_locations = separate_words (text_file); ostream_iterator output (cout, "/ n"); cout << "----------- About to generate text read ------------ / n"; copy (text_file-> begin (), text_file-> end () , Output); cout << "----------- About to Word and Location Data ------------- / n"; Display_Text_locations (Text_locations); Return 0 Vector * retrieve_text () {string file_name; cout << "

Please enter file name: "; cin >> file_name; ifstream infile (file_name.c_str (), ios :: in); if (! infile) {cerr <<" OOPS! Unable to open file "<< file_name <<" - bailing out! / n "; exit (-1);} else cout <<" / n "; vector * lines_of_text = new vector ; string textline; typedef pair stats; stats maxline; int lineenum = 0; while (getLine (infile, textline, '/ n')) {cout << "line read: << TextLine <<" / n "; if (Maxline.First < TextLine.LENGTH ()) {Maxline.First = TextLine.Length (); Maxline.Second = linenum;} lines_of_text-> push_back (textline); LINENUM ;} cout << "/ n"; cout << "Number of Lines : "<< Lines_of_text-> size () <<" / n "; cout <<" Maximum Length: "<< maxline.first <<" / n "; cout <<" LONGEST line: "<< (* line_of_text [MaxLine.Second] << "/ n"; return lines_of_text;} text_loc * Separate_words (const vector * text_file) { // OK: now have all the line. Wish to grab the // Individual Words: Look for Blanks: Vector * Words = New Vector ; Vector * locations = new vector ; / / For the input text files given in the book text_file-> size () should be 6, because the string vector is 6 rows //, so for the for loop is for (Short line_pos = 0; line_pos <6; line_pos ), a total circulation 6 Supreme // Generation per row for (short line_pos = 0; line_pos <

Text_file-> size (); line_pos ) {short word_pos = 0; string textLine = (* text_file) [line_pos]; cout << "textline: << textLine << endl; string :: size_type eol = textline.Length ); // This is the length of a line, even the spaces and symbols don't let String :: size_type pos = 0, prev_pos = 0; // Good, let us start counting.

While ((POS = TextLine.Find_First_OF ('', POS)))! = String :: Npos) {// For example: Alice Emma Has Long Flowing Red Hair. Her Daddy Says // EOL: 52 POS: 5 line: 0 Word: 0 Substring: Alice means that number to 52, the first line ends, the number of spaces appears (remember that Mr. Lippman said to start from 0), is the 0th OK, the first word is marked, // Location is 0, the content is Alice, then put the alice in Words, Words is a string vector, think about why // should be put into the vector, because you want to give a separate The word code, such as No. 0, No. 1, No. 2 ....... COUT << "eol:" << eol << "<<" POS: "<< POS <<" "<<" Line: "<< line_pos <<" "<<" Word: "<< Word_POS <<" << "Substring: << TextLine.Substr (prev_pos, pos-prev_pos <<" / n "; Words- > Push_back (TextLine.Substr (prev_pos, pos - prev_pos)); // Locations is a pair vector, which will become {(0, 0) ...} Locations-> push_back ( Make_pair (line_pos, word_pos); Word_POS ; POS ; prev_pos = POS;} Cout << "Last Word on line substring:" << TextLine.Substr (Prev_pos, POS-PREV_POS) << "/ n"; Words-> Push_Back (TextLine.Substr (Prev_pos, POS - Prev_pos); Locations-> Push_Back (make_pair (line_pos, word_pos);} Return New Text_loc (Words, Locations); // (Alice, (0, 0)) ........} // text_loc is a (Vector <

String> *, vector ) Pair // Display_text_locations First, Words and Locations first judge the matching situation of both, because each correspondence should have a location, in short, Ji one for one, because they are // vectorvoid display_text_locations (text_loc * text_locations) {vector * text_words = text_locations-> first; vector * text_locs = text_locations-> second; register int elem_cnt = text_words-> size (); If (elem_cnt! = Text_locs-> size ()) {CERR << "OOPS! INTERNAL ERROR: WORD AND POSITION vectorS" << "" "" Words: << ELEM_CNT < <"<<" LOCS: "<< Text_locs-> size () <<" - bailing out! / N "; exit (-2);} for (int = 0; ix

转载请注明原文地址:https://www.9cbs.com/read-95145.html

New Post(0)