?? 021.htm
字號:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->Learn C++ in 21 Days-->Day 21</TITLE>
<META NAME="888" CONTENT=" Learn C++ in 21 Days Day 21">
<META NAME="888" CONTENT=" - Learn C++ in 21 Days - Day 21">
<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋體"}
.tt2 {font: 12pt/15pt "宋體"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<p align="center"><script src="../../../1.js"></script> </p>
<TD CLASS="tt3" VALIGN="top" width="8%" bgcolor="#ffffff"><strong><A HREF="index.html"><FONT style="FONT-SIZE: 9pt">后一頁</font></A><BR>
<A HREF="020.htm"><FONT style="FONT-SIZE: 9pt">前一頁</font></A><BR>
<A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目錄</font></A><BR>
<TD class="tt2" bgcolor="#ffffff" width="100%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷體_GB2312">Day 21</FONT></B></center>
<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>
<BR>
<P>
<HR SIZE="4">
<BR>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 21</FONT></H2>
<BR>
<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 introduction
to C++. By now you should have a solid understanding of C++, but in modern programming
there is always more to learn. This chapter will fill in some missing details and
then set the course for continued study.</P>
<P>Today you will learn
<BR>
<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>
<BR>
<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 include
additional libraries as well. Libraries are sets of functions that can be linked
into 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, much
as you did by writing <TT>#include <iostream.h></TT> in many of the examples
in this book. The angle brackets around the filename are a signal to the compiler
to look in the directory where you keep the header files for your compiler's standard
libraries.</P>
<P>There are dozens of libraries, covering everything from file manipulation to setting
the date and time to math functions. Today I will review just a few of the most popular
functions and classes in the standard library that have not yet been covered in this
book.
<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 perhaps
the function <TT>strlen()</TT> called most often. <TT>strlen()</TT> returns the length
of a null-terminated string. Listing 21.1 illustrates its use.</P>
<BR>
<P><A NAME="Heading5"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.1. strlen().</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1: #include <iostream.h>
2: #include <string.h>
3:
4: int main()
5: {
6: char buffer80];
7: do
8: {
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" font style="font-size:10pt">
Output: Enter a string up to 80 characters: This sentence has 31 characters
Your string is 31 characters long.
Enter a string up to 80 characters: This sentence no verb
Your string is 21 characters long.
Enter a string up to 80 characters:
Your string is 0 characters long.
<BR>
Done.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 6, a character buffer is
created, and on line 9 the user is prompted to enter a string. As long as the user
enters 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 long
as 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-style
null-terminated strings have become less important in C++; typically, string manipulation
is 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 Listing
21.2.</P>
<BR>
<P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.2. Using strcpy.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">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()
<BR>
String One: Test of strcpy()
String Two:
copying...
<BR>
String One: Test of strcpy()
String Two: Test of strcpy()
<BR>
Done</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>Two C-style null-terminated strings
are declared on lines 6 and 7. They are initialized to empty on lines 9 and 10, and
their 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, and
only <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 first
parameter 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 its
solution are illustrated in Listing 21.3.</P>
<BR>
<P><A NAME="Heading8"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 21.3. Using strncpy().</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">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;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -