📄 chapter02.html
字号:
character arrays and stores the ASCII equivalent for each character in this
storage. The compiler automatically terminates this array of characters with an
extra piece of storage containing the value 0 to indicate the end of the
character array. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Inside a character array, you can insert
special characters by using <I>escape sequences<A NAME="Index352"></A></I>.
These consist of a backslash (<B>\</B>) followed by a special code. For example
<B>\n</B> means <A NAME="Index353"></A>newline. Your compiler manual or local C
guide gives a complete set of escape sequences; others include <B>\t</B>
(<A NAME="Index354"></A>tab), <B>\\</B> (<A NAME="Index355"></A>backslash), and
<B>\b</B> (<A NAME="Index356"></A>backspace).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice that the statement can continue
over multiple lines, and that the entire statement terminates with a
semicolon</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Character array arguments and constant
numbers are mixed together in the above <B>cout</B> statement. Because the
operator <B><<</B> is overloaded<A NAME="Index357"></A> with a variety of
meanings when used with <B>cout</B>, you can send <B>cout</B> a variety of
different arguments and it will “figure out what to do with the
message.”</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Throughout this book you’ll notice
that the first line of each file will be a comment that starts with the
characters that start a comment (typically <B>//</B>), followed by a colon, and
the last line of the listing will end with a comment followed by
‘<B>/:~</B>’. This is a technique I use to allow easy extraction of
information from code files (the program to do this can be found in volume two
of this book, at <I>www.BruceEckel.com</I>). The first line also has the name
and location of the file, so it can be referred to in text and in other files,
and so you can easily locate it in the source code for this book (which is
downloadable from
<I>www.BruceEckel.com</I>).</FONT><A NAME="_Toc462979726"></A><A NAME="_Toc472654730"></A><BR></P></DIV>
<A NAME="Heading89"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Running the compiler<A NAME="Index358"></A> </H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">After downloading and unpacking the
book’s source code, find the program in the subdirectory <B>CO2</B>.
Invoke the compiler with <B>Hello.cpp</B> as the argument. For simple, one-file
programs like this one, most compilers will take you all the way through the
process. For example, to use the GNU C++ compiler (which is freely available on
the Internet), you write:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>g++ Hello.cpp</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Other compilers will have a similar
syntax; consult your compiler’s documentation for
details.</FONT><A NAME="_Toc462979727"></A><A NAME="_Toc472654731"></A><BR></P></DIV>
<A NAME="Heading90"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
More about iostreams </H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So far you have seen only the most
rudimentary aspect of the iostreams class. The output formatting available with
iostreams also includes features such as number formatting in decimal, octal,
and hexadecimal. Here’s another example of the use of
iostreams:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:Stream2.cpp</font>
<font color=#009900>// More streams features</font>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
<font color=#009900>// Specifying formats with manipulators:</font>
cout << <font color=#004488>"a number in decimal: "</font>
<< dec << 15 << endl;
cout << <font color=#004488>"in octal: "</font> << oct << 15 << endl;
cout << <font color=#004488>"in hex: "</font> << hex << 15 << endl;
cout << <font color=#004488>"a floating-point number: "</font>
<< 3.14159 << endl;
cout << <font color=#004488>"non-printing char (escape): "</font>
<< <font color=#0000ff>char</font>(27) << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This example shows the iostreams class
printing numbers in decimal, octal, and hexadecimal using iostream
<I>manipulators</I> <A NAME="Index359"></A>(which don’t print anything,
but change the state of the output stream). The formatting of floating-point
numbers is determined automatically by the compiler. In addition, any character
can be sent to a stream object using a <I>cast</I> to a
<A NAME="Index360"></A><A NAME="Index361"></A><B>char</B> (a <B>char</B> is a
data type that holds single characters). This <I>cast </I>looks like a function
call: <B>char( )</B>, along with the character’s ASCII value. In the
program above, the <B>char(27)</B> sends an “escape” to
<B>cout</B>.</FONT><A NAME="_Toc462979728"></A><A NAME="_Toc472654732"></A><BR></P></DIV>
<A NAME="Heading91"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Character array concatenation</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An important feature of the C
preprocessor is <A NAME="Index362"></A><A NAME="Index363"></A><I>character array
concatenation<A NAME="Index364"></A></I>. This feature is used in some of the
examples in this book. If two quoted character arrays are adjacent, and no
punctuation is between them, the compiler will paste the character arrays
together into a single character array. This is particularly useful when code
listings have width restrictions:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:Concat.cpp</font>
<font color=#009900>// Character array Concatenation</font>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
cout << <font color=#004488>"This is far too long to put on a "</font>
<font color=#004488>"single line but it can be broken up with "</font>
<font color=#004488>"no ill effects\nas long as there is no "</font>
<font color=#004488>"punctuation separating adjacent character "</font>
<font color=#004488>"arrays.\n"</font>;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At first, the code above can look like an
error because there’s no familiar semicolon at the end of each line.
Remember that C and C++ are free-form languages, and although you’ll
usually see a semicolon at the end of each line, the actual requirement is for a
semicolon at the end of each statement, and it’s possible for a
<A NAME="Index365"></A>statement to continue over several
lines.</FONT><A NAME="_Toc462979729"></A><A NAME="_Toc472654733"></A><BR></P></DIV>
<A NAME="Heading92"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Reading input<A NAME="Index366"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The iostreams classes provide the ability
to read input. The object used for
<A NAME="Index367"></A><A NAME="Index368"></A>standard input is
<A NAME="Index369"></A><B>cin</B> (for “console input”). <B>cin</B>
normally expects input from the console, but this input can be redirected from
other sources. An example of redirection is shown later in this
chapter.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The iostreams operator used with
<B>cin</B> is <B>>></B>. This operator waits for the same kind of input as
its argument. For example, if you give it an integer argument, it waits for an
integer from the console. Here’s an example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:Numconv.cpp</font>
<font color=#009900>// Converts decimal to octal and hex</font>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
<font color=#0000ff>int</font> number;
cout << <font color=#004488>"Enter a decimal number: "</font>;
cin >> number;
cout << <font color=#004488>"value in octal = 0"</font>
<< oct << number << endl;
cout << <font color=#004488>"value in hex = 0x"</font>
<< hex << number << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This program converts a number typed in
by the user into octal and hexadecimal
representations.</FONT><A NAME="_Toc472654734"></A><BR></P></DIV>
<A NAME="Heading93"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Calling other programs<A NAME="Index370"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While the typical way to use a program
that reads from standard input and writes to standard output is within a Unix
shell script or DOS batch file, any program can be called from inside a C or C++
program using the Standard C <B>system( )</B>
function<A NAME="Index371"></A>, which is declared in the header file
<B><cstdlib></B>:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:CallHello.cpp</font>
<font color=#009900>// Call another program</font>
#include <cstdlib> <font color=#009900>// Declare "system()"</font>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
system(<font color=#004488>"Hello"</font>);
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To use the <B>system( )</B>
function, you give it a character array that you would normally type at the
operating system command prompt. This can also include command-line arguments,
and the character array can be one that you fabricate at run time (instead of
just using a static character array as shown above). The command executes and
control returns to the program.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This program shows you how easy it is to
use plain C library functions in C++; just include the header file and call the
function. This upward <A NAME="Index372"></A>compatibility from C to C++ is a
big advantage if you are learning the language starting from a background in
C.</FONT><A NAME="_Toc462979731"></A><A NAME="_Toc472654735"></A><BR></P></DIV>
<A NAME="Heading94"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Introducing strings</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While a character array can be fairly
useful, it is quite limited. It’s simply a group of characters in memory,
but if you want to do anything with it you must manage all the little details.
For example, the size of a quoted character array is fixed at compile time. If
you have a character array and you want to add some more characters to it,
you’ll need to understand quite a lot (including dynamic memory
management, character array copying, and concatenation) before you can get your
wish. This is exactly the kind of thing we’d like to have an object do for
us.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Standard C++
<A NAME="Index373"></A><B>string</B> class is designed to take care of (and
hide) all the low-level manipulations of character arrays that were previously
required of the C programmer. These manipulations have been a constant source of
time-wasting and errors since the inception of the C language. So, although an
entire chapter is devoted to the <B>string</B> class in Volume 2 of this book,
the <B>string</B> is so important and it makes life so much easier that it will
be introduced here and used in much of the early part of the
book.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To use <B>string</B>s you include the C++
header file <B><string></B>. The <B>string</B> class is in the namespace
<B>std</B> so a <B>using</B> directive is necessary. Because of operator
overloading, the syntax for using <B>string</B>s is quite
intuitive:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:HelloString
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -