📄 ch21.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><!-- This document was created from RTF source by rtftohtml version 3.0.1 --> <META NAME="GENERATOR" Content="Symantec Visual Page 1.0"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself C++ in 21 Days</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><A HREF="ch20.htm"><IMG SRC="../buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="http://www.mcp.com/sams"><IMGSRC="../buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch21rv3.htm"><IMG SRC="../buttons/BLANNEXT.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1><H1></H1><UL> <LI><A HREF="#Heading1">Day 21</A> <UL> <LI><A HREF="#Heading2">Whats Next</A> <UL> <LI><A HREF="#Heading3">The Standard Libraries</A> <LI><A HREF="#Heading4">String</A> <LI><A HREF="#Heading5">Listing 21.1. strlen().</A> <UL> <LI><A HREF="#Heading6">strcpy() and strncpy()</A> </UL> <LI><A HREF="#Heading7">Listing 21.2. Using strcpy.</A> <LI><A HREF="#Heading8">Listing 21.3. Using strncpy().</A> <UL> <LI><A HREF="#Heading9">strcat() and strncat()</A> </UL> <LI><A HREF="#Heading10">Listing 21.4. Using strcat() and strncat().</A> <UL> <LI><A HREF="#Heading11">Other String Functions</A> </UL> <LI><A HREF="#Heading12">Time and Date</A> <LI><A HREF="#Heading13">Listing 21.5. Using ctime().</A> <LI><A HREF="#Heading14">stdlib</A> <LI><A HREF="#Heading15">Listing 21.6. Using atoi() and related functions.</A> <UL> <LI><A HREF="#Heading16">qsort()</A> </UL> <LI><A HREF="#Heading17">Listing 21.7. Using qsort().</A> <UL> <LI><A HREF="#Heading18">Other Libraries</A> </UL> <LI><A HREF="#Heading19">Bit Twiddling</A> <UL> <LI><A HREF="#Heading20">Operator AND</A> <LI><A HREF="#Heading21">Operator OR</A> <LI><A HREF="#Heading22">Operator Exclusive OR</A> <LI><A HREF="#Heading23">The Complement Operator</A> <LI><A HREF="#Heading24">Setting Bits</A> <LI><A HREF="#Heading25">Clearing Bits</A> <LI><A HREF="#Heading26">Flipping Bits</A> <LI><A HREF="#Heading27">Bit Fields</A> </UL> <LI><A HREF="#Heading28">Listing 21.8. Using bit fields</A><A HREF="#Heading29">.</A> <LI><A HREF="#Heading30">Style</A> <UL> <LI><A HREF="#Heading31">Indenting</A> <LI><A HREF="#Heading32">Braces</A> <LI><A HREF="#Heading33">Long Lines</A> <LI><A HREF="#Heading34">switch Statements</A> <LI><A HREF="#Heading35">Program Text</A> <LI><A HREF="#Heading36">Identifier Names</A> <LI><A HREF="#Heading37">Spelling and Capitalization of Names</A> <LI><A HREF="#Heading38">Comments</A> <LI><A HREF="#Heading39">Access</A> <LI><A HREF="#Heading40">Class Definitions</A> <LI><A HREF="#Heading41">include Files</A> <LI><A HREF="#Heading42">assert()</A> <LI><A HREF="#Heading43">const</A> </UL> <LI><A HREF="#Heading44">Next Steps</A> <UL> <LI><A HREF="#Heading45">Where to Get Help and Advice</A> <LI><A HREF="#Heading46">Required Reading</A> <LI><A HREF="#Heading47">Magazines</A> <LI><A HREF="#Heading48">Staying in Touch</A> </UL> <LI><A HREF="#Heading49">Summary</A> <LI><A HREF="#Heading50">Q&A</A> <UL> <LI><A HREF="#Heading51">Quiz</A> <LI><A HREF="#Heading52">Exercises</A> </UL> </UL> </UL></UL><P><HR SIZE="4"><H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 21</FONT></H2><H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Whats Next</FONT></H2><P>Congratulations! You are nearly done with a full three-week intensive introductionto C++. By now you should have a solid understanding of C++, but in modern programmingthere is always more to learn. This chapter will fill in some missing details andthen set the course for continued study.</P><P>Today you will learn<UL> <LI>What the standard libraries are. <P> <LI>How to manipulate individual bits and use them as flags. <P> <LI>What the next steps are in learning to use C++ effectively.</UL><H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">The Standard Libraries</FONT></H3><P>Every implementation of C++ includes the standard libraries, and most includeadditional libraries as well. Libraries are sets of functions that can be linkedinto your code. You've already used a number of standard library functions and classes,most notably from the <TT>iostreams</TT> library.</P><P>To use a library, you typically include a header file in your source code, muchas you did by writing <TT>#include <iostream.h></TT> in many of the examplesin this book. The angle brackets around the filename are a signal to the compilerto look in the directory where you keep the header files for your compiler's standardlibraries.</P><P>There are dozens of libraries, covering everything from file manipulation to settingthe date and time to math functions. Today I will review just a few of the most popularfunctions and classes in the standard library that have not yet been covered in thisbook.<H3 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">String</FONT></H3><P>The most popular library is almost certainly the string library, with perhapsthe function <TT>strlen()</TT> called most often. <TT>strlen()</TT> returns the lengthof a null-terminated string. Listing 21.1 illustrates its use.</P><P><A NAME="Heading5"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.1. strlen().</B></FONT><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: #include <string.h>3:4: int main()5: {6: char buffer80];7: do8: {9: cout << "Enter a string up to 80 characters: ";10: cin.getline(buffer,80);11: cout << "Your string is " << strlen(buffer);12: cout << " characters long." << endl;13: } while (strlen(buffer));14: cout << "\nDone." << endl;15: return 0;<TT>16: }</TT></FONT><FONT COLOR="#0066FF">Output: Enter a string up to 80 characters: This sentence has 31 charactersYour string is 31 characters long.Enter a string up to 80 characters: This sentence no verbYour string is 21 characters long.Enter a string up to 80 characters:Your string is 0 characters long.Done.</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 6, a character buffer iscreated, and on line 9 the user is prompted to enter a string. As long as the userenters a string, the length of the string is reported on line 11.<BR><BR>Note the test in the <TT>do...while()</TT> statement: <TT>while (strlen(buffer))</TT>.Since <TT>strlen()</TT> will return <TT>0</TT> when the buffer is empty, and since<TT>0</TT> evaluates <TT>FALSE</TT>, this <TT>while</TT> loop will continue as longas there are any characters in the buffer.<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">strcpy() and strncpy()</FONT></H4><P>The second most popular function in <TT>string.h</TT> probably was <TT>strcpy()</TT>,which copied one string to another. This may now be diminished somewhat as C-stylenull-terminated strings have become less important in C++; typically, string manipulationis done from within a vendor-supplied or user-written <TT>string</TT> class. Nonetheless,your <TT>string</TT> class must support an assignment operator and a copy constructor,and often these are implemented using <TT>strcpy()</TT>, as illustrated in Listing21.2.</P><P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.2. Using strcpy.</B></FONT><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: #include <string.h>3:4: int main()5: {6: char stringOne80];7: char stringTwo80];8:9: stringOne0]='\0';10: stringTwo0]='\0';11:12: cout << "String One: " << stringOne << endl;13: cout << "String Two: " << stringTwo << endl;14:15: cout << "Enter a string: ";16: cin.getline(stringOne,80);17:18: cout << "\nString One: " << stringOne << endl;19: cout << "String Two: " << stringTwo << endl;20:21: cout << "copying..." << endl;22: strcpy(stringTwo,stringOne);23:24: cout << "\nString One: " << stringOne << endl;25: cout << "String Two: " << stringTwo << endl;26: cout << "\nDone " << endl;27: return 0;<TT>28: }</TT></FONT><FONT COLOR="#0066FF">Output: String One:String Two:Enter a string: Test of strcpy()String One: Test of strcpy()String Two:copying...String One: Test of strcpy()String Two: Test of strcpy()Done</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis: </B></FONT>Two C-style null-terminated stringsare declared on lines 6 and 7. They are initialized to empty on lines 9 and 10, andtheir values are printed on lines 12 and 13. The user is prompted to enter a string,and the result is put in <TT>stringOne</TT>; the two strings are printed again, andonly <TT>stringOne</TT> has the input. <TT>Strcpy()</TT> is then called, and <TT>stringOne</TT>is copied into <TT>stringTwo</TT>.<BR><BR>Note that the syntax of <TT>strcpy()</TT> can be read as "copy into the firstparameter the string in the second parameter." What happens if the target string(<TT>stringTwo</TT>) is too small to hold the copied string? This problem and itssolution are illustrated in Listing 21.3.</P><P><A NAME="Heading8"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.3. Using strncpy().</B></FONT><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: #include <string.h>3:4: int main()5: {6: char stringOne[80];7: char stringTwo[10];8: char stringThree[80];9:10: stringOne[0]='\0';11: stringTwo[0]='\0';12: stringThree[0]='\0';13:14: cout << "String One: " << stringOne << endl;15: cout << "String Two: " << stringTwo << endl;16: cout << "String Three: " << stringThree << endl;17:18: cout << "Enter a long string: ";19: cin.getline(stringOne,80);20: strcpy(stringThree,stringOne);21: // strcpy(stringTwo,stringOne);22:23: cout << "\nString One: " << stringOne << endl;24: cout << "String Two: " << stringTwo << endl;25: cout << "String Three: " << stringThree << endl;26:27: strncpy(stringTwo,stringOne,9);28:29: cout << "\nString One: " << stringOne << endl;30: cout << "String Two: " << stringTwo << endl;31: cout << "String Three: " << stringThree << endl;32:33: stringTwo[9]='\0';34:35: cout << "\nString One: " << stringOne << endl;36: cout << "String Two: " << stringTwo << endl;37: cout << "String Three: " << stringThree << endl;38: cout << "\nDone." << endl;39: return 0;<TT>40: }</TT></FONT><FONT COLOR="#0066FF">Output: String One:String Two:String Three:Enter a long string: Now is the time for all...String One: Now is the time for all...String Two:String Three: Now is the time for all...String One: Now is the time for all...String Two: Now is th_+||String Three: Now is the time for all...String One: Now is the time for all...String Two: Now is thString Three: Now is the time for all...Done.</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>On lines 6, 7, and 8, threestring buffers are declared. Note that <TT>stringTwo</TT> is declared to be only10 characters, while the others are 80. All three are initialized to zero lengthon lines 10 to 12 and are printed on lines 14 to 16.<BR><BR>The user is prompted to enter a string, and that string is copied to string threeon line 20. Line 21 is commented out; copying this long string to <TT>stringTwo</TT>caused a crash on my computer because it wrote into memory that was critical to theprogram.</P><P>The standard function <TT>strcpy()</TT> starts copying at the address pointedto by the first parameter (the array name), and it copies the entire string withoutensuring that you've allocated room for it!</P><P>The standard library offers a second, safer function, <TT>strncpy()</TT>, whichcopies only a specified number of characters to the target string. The <TT>n</TT>in the middle of the function name <TT>strncpy()</TT> stands for number. This isa convention used throughout the standard libraries.</P><P>On line 27, the first nine characters of <TT>stringOne</TT> are copied to <TT>stringTwo</TT>and the result is printed. Because <TT>strncpy()</TT> does not put a null at theend of the copied string, the result is not what was intended. Note that <TT>strcpy()</TT>does null-terminate the copied string, but <TT>strncpy()</TT> does not, just to keeplife interesting.</P><P>The null is added on line 33, and the strings are then printed a final time.<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">strcat() and strncat()</FONT></H4><P>Related to <TT>strcpy()</TT> and <TT>strncpy()</TT> are the standard functions<TT>strcat()</TT> and <TT>strncat()</TT>. The former concatenates one string to another;that is, it appends the string it takes as its second parameter to the end of thestring it takes as its first parameter. <TT>strncat()</TT>, as you might expect,appends the first <TT>n</TT> characters of one string to the other. Listing 21.4illustrates their use.</P><P><A NAME="Heading10"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.4. Usingstrcat() and strncat().</B></FONT><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: #include <string.h>3:4:5: int main()6: {7: char stringOne[255];8: char stringTwo[255];9:10: stringOne[0]='\0';11: stringTwo[0]='\0';12:13: cout << "Enter a string: ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -