📄 ch03.htm
字号:
system, or a 3D game engine, just don't worry about this stuff. Of course, if you
love worrying about these issues, then worry away. I suppose someone has to. Otherwise,
relax. There is almost no way to tell from looking at the code what the compiler
is going to do, and 90 percent of the time the compiler is smart enough to generate
optimal code unless you explicitly tell it to go out of its way, as I do in the second
example.</P>
<P>If you are a beginning- or intermediate-level programmer,
someone is going to
tell you that you should pull your hair out worrying about optimization issues like
this. Personally, I think there are more important subjects to which you can dedicate
your time. Eliminating one constructor call might save a few
nanoseconds in execution
time, but nobody is going to notice it. Learn to separate the petty issues from the
serious issues.</P>
<P>Most importantly of all, wait until you have a program up and running before you
decide whether or not you have a
performance bottleneck. If the program is too slow,
use a profiler to find out where the bottlenecks are. Ninety-eight percent of the
time the bottlenecks are in a few isolated places in the code, and fixing those spots
clears up the problem. Don't
spend two weeks looking for every place there is an
extra constructor call only to find it improves your performance by less than one
percent. Wait till you have a problem, find the problem, and then fix it. If it turns
out that you are in a loop, and
are making extra constructor calls in the loop, and
that by doing some hand waving to get rid of the calls you improve program performance
by 10 percent, then great. But don't spend days working on "optimizations"
that end up improving
performance by only one or two percent.
<H3><FONT COLOR="#000077">AnsiString Comparison Operators</FONT></H3>
<P>Comparison operators are useful if you want to alphabetize strings. For instance,
asking if <TT>StringOne</TT> is smaller than
<TT>StringTwo</TT> is a means of finding
out whether <TT>StringOne</TT> comes before <TT>StringTwo</TT> in the alphabet. For
instance, if <TT>StringOne</TT> equals <TT>"Dole"</TT> and <TT>StringTwo</TT>
equals <TT>"Clinton"</TT>,
<TT>StringOne</TT> is larger than <TT>StringTwo</TT>
because it comes later in the alphabet. (At last, a politically controversial string
comparison analysis!)</P>
<P>Here are the <TT>AnsiString</TT> class comparison operators and methods:</P>
<PRE><FONT COLOR="#0066FF">bool __fastcall operator ==(const AnsiString& rhs) const;
bool __fastcall operator !=(const AnsiString& rhs) const;
bool __fastcall operator <(const AnsiString& rhs) const;
bool __fastcall operator
>(const AnsiString& rhs) const;
bool __fastcall operator <=(const AnsiString& rhs) const;
bool __fastcall operator >=(const AnsiString& rhs) const;
int __fastcall AnsiCompare(const AnsiString& rhs) const;
int __fastcall
AnsiCompareIC(const AnsiString& rhs) const; //ignorecase
</FONT></PRE>
<P>The UsingAnsiString program on this book's CD-ROM shows how to use these operators.
For instance, the following method from that program shows how to use the equals
operator:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall TForm1::Equals1Click(TObject *Sender)
{
AnsiString StringOne, StringTwo;
InputDialog->GetStringsFromUser(&StringOne, &StringTwo);
if (StringOne == StringTwo)
Memo1->Text = "\"" + StringOne + "\" is equal to \"" + StringTwo + "\"";
else
Memo1->Text = "\"" + StringOne + "\" is not equal to \"" + StringTwo +
"\"";
}
</FONT></PRE>
<P>Here is an example of using the "smaller than" operator:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall TForm1::SmallerThan1Click(TObject *Sender)
{
AnsiString String1, String2;
InputDialog->GetStringsFromUser(&String1, &String2);
if (String1 < String2)
Memo1->Text = "\"" + String1 + "\" is smaller than \"" + String2 + "\"";
else
Memo1->Text
= "\"" + String1 + "\" is not smaller than \"" + String2 + "\"";
}
</FONT></PRE>
<P>The <TT>AnsiCompareIC</TT> function is useful if you want to ignore the case of
words when comparing them. This
method returns zero if the strings are equal, a positive
number if the string calling the function is larger than the string to which it is
being compared, and negative number if it is not larger than the string to which
it is being compared:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall TForm1::IgnoreCaseCompare1Click(TObject *Sender)
{
AnsiString String1, String2;
InputDialog->GetStringsFromUser(&String1, &String2);
if (String1.AnsiCompareIC(String2) == 0)
Memo1->Text = "\"" + String1 + "\" equals \"" + String2 + "\"";
else if (String1.AnsiCompareIC(String2) > 0)
Memo1->Text = "\"" + String1 + "\" is larger than
\"" + String2 + "\"";
else if (String1.AnsiCompareIC(String2) < 0)
Memo1->Text = "\"" + String1 + "\" is smaller than \"" + String2 + "\"";
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
}
</FONT></PRE>
<P>Consider the following chart:
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="84" ALIGN="LEFT"><B><TT>StringOne</TT></B></TD>
<TD WIDTH="100"
ALIGN="LEFT"><B><TT>StringTwo</TT></B></TD>
<TD ALIGN="LEFT"><B>Result</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="84" ALIGN="LEFT">England</TD>
<TD WIDTH="100" ALIGN="LEFT">England</TD>
<TD ALIGN="LEFT">Returns zero</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="84" ALIGN="LEFT">England</TD>
<TD WIDTH="100" ALIGN="LEFT">France</TD>
<TD ALIGN="LEFT">Return a negative number</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="84" ALIGN="LEFT"
VALIGN="TOP">France</TD>
<TD WIDTH="100" ALIGN="LEFT" VALIGN="TOP">England</TD>
<TD ALIGN="LEFT">Returns a positive number</TD>
</TR>
</TABLE>
</P>
<DL>
<DD>
</DL>
<H3><FONT COLOR="#000077">Using the Pos Method</FONT></H3>
<P>The <TT>Pos</TT>
method is used to find the offset of a substring within a larger
string. Here is a simple example from the UsingAnsiString program of how to use the
<TT>Pos</TT> function:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall
TForm1::SimpleString1Click(TObject *Sender)
{
AnsiString S = "Sammy";
Memo1->Text = S;
int i = S.Pos("mm");
S.Delete(i + 1, 2);
Memo1->Lines->Add(S);
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">}
</FONT></PRE>
<P>This code creates an <TT>AnsiString</TT> initialized to the name <TT>"Sammy"</TT>.
It then searches through the string for the place where the substring <TT>"mm"</TT>
occurs, and returns that index so that it can
be stored in the variable i. I then
use the index as a guide when deleting the last two characters from the string, thereby
transforming the word <TT>"Sammy"</TT> to the string <TT>"Sam"</TT>.</P>
<P>Here is a similar case, except
that this time code searches for a tab rather than
an ordinary substring:</P>
<PRE><FONT COLOR="#0066FF">
void __fastcall TForm1::Pos1Click(TObject *Sender)
{
AnsiString S("Sammy \t Mike");
Memo1->Text = S;
AnsiString Temp =
Format("The tab is in the %dth position",
OPENARRAY(TVarRec, (S.Pos(`\t'))));
Memo1->Lines->Add(Temp);
}
</FONT></PRE>
<P>The code in this example first initializes the <TT>AnsiString</TT> <TT>S</TT>
to a string that
contains a tab, and then searches through the string and reports
the offset of the tab: "The tab is in the 7th position." The <TT>Format</TT>
function shown here works in the same fashion as <TT>sprintf</TT>. In fact, the following
code
would have the same result as the <TT>Pos1Click</TT> method shown previously:</P>
<PRE><FONT COLOR="#0066FF">AnsiString S("Sammy \x009 Mike");
Memo1->Text = S;
char Temp[75];
sprintf(Temp, "The tab is in the %dth position",
S.Pos(`\t'));
Memo1->Lines->Add(Temp);
</FONT></PRE>
<H3><FONT COLOR="#000077">Escape Sequences in C++</FONT></H3>
<P>C++ uses a series of escape sequences to represent special characters such as
tabs, backspaces, and so on. If you are not
familiar with C++, you might find the
following table of escape sequences useful:<BR>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD HEIGHT="17" ALIGN="LEFT"><B>Human readable name</B></TD>
<TD HEIGHT="17" ALIGN="LEFT"><B>Escape
sequence</B></TD>
<TD HEIGHT="17" ALIGN="LEFT"><B>Hex representation</B></TD>
</TR>
<TR>
<TD HEIGHT="17" ALIGN="LEFT">Bell</TD>
<TD HEIGHT="17" ALIGN="LEFT"><TT>\a</TT></TD>
<TD HEIGHT="17" ALIGN="LEFT"><TT>\x007</TT></TD>
</TR>
<TR
ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Backspace</TD>
<TD ALIGN="LEFT"><TT>\b</TT></TD>
<TD ALIGN="LEFT"><TT>\x008</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Tab</TD>
<TD ALIGN="LEFT"><TT>\t</TT></TD>
<TD
ALIGN="LEFT"><TT>\x009</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Newline</TD>
<TD ALIGN="LEFT"><TT>\n</TT></TD>
<TD ALIGN="LEFT"><TT>\x00A</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Form Feed</TD>
<TD ALIGN="LEFT"><TT>\f</TT></TD>
<TD ALIGN="LEFT"><TT>\x00C</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Carriage return</TD>
<TD ALIGN="LEFT"><TT>\r</TT></TD>
<TD ALIGN="LEFT"><TT>\x00D</TT></TD>
</TR>
<TR ALIGN="LEFT"
rowspan="1">
<TD ALIGN="LEFT">Double quote</TD>
<TD ALIGN="LEFT"><TT>\"</TT></TD>
<TD ALIGN="LEFT"><TT>\x022</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Single quote</TD>
<TD ALIGN="LEFT"><TT>\'</TT></TD>
<TD
ALIGN="LEFT"><TT>\x027</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Backslash</TD>
<TD><TT>\\</TT></TD>
<TD><TT>\x05C</TT></TD>
</TR>
</TABLE>
<DL>
<DD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -