📄 chap04.htm
字号:
<font color=#0000ff>void</font> replaceChars(string& modifyMe,
string findMe, string newChars){
<font color=#009900>// Look in modifyMe for the "find string"</font>
<font color=#009900>// starting at position 0</font>
<font color=#0000ff>int</font> i = modifyMe.find(findMe, 0);
<font color=#009900>// Did we find the string to replace?</font>
<font color=#0000ff>if</font>(i != string::npos)
<font color=#009900>// Replace the find string with newChars</font>
modifyMe.replace(i,newChars.size(),newChars);
}
<font color=#0000ff>int</font> main() {
string bigNews =
<font color=#004488>"I thought I saw Elvis in a UFO. "</font>
<font color=#004488>"I have been working too hard."</font>;
string replacement(<font color=#004488>"wig"</font>);
string findMe(<font color=#004488>"UFO"</font>);
<font color=#009900>// Find "UFO" in bigNews and overwrite it:</font>
replaceChars(bigNews, findMe, replacement);
cout << bigNews << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now the last line of output from
<B>replace.cpp</B> looks like this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>I thought I saw Elvis in a wig. I have been
working too hard.</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If replace doesn’t find the search
string, it returns <B>npos</B>. <B>npos</B> is a static constant member of the
<B>basic_string</B> class.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Unlike <B>insert( )</B>,
<B>replace( )</B> won’t grow the <B>string</B>’s storage space
if you copy new characters into the middle of an existing series of array
elements. However, it <I>will</I> grow the storage space if you make a
“replacement” that writes beyond the end of an existing array.
Here’s an example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C04:ReplaceAndGrow.cpp</font>
<font color=#009900>//{L} ../TestSuite/Test</font>
#include <string>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
string bigNews(<font color=#004488>"I saw Elvis in a UFO. "</font>
<font color=#004488>"I have Been working too hard."</font>);
string replacement(<font color=#004488>"wig"</font>);
<font color=#009900>// The first arg says "replace chars </font>
<font color=#009900>// beyond the end of the existing string":</font>
bigNews.replace(bigNews.size(),
replacement.size(), replacement);
cout << bigNews << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The call to <B>replace( ) </B>begins
“replacing” beyond the end of the existing array. The output looks
like this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>I saw Elvis in a UFO. I have
been working too hard.wig</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice that <B>replace( )</B>
expands the array to accommodate the growth of the string due to
“replacement” beyond the bounds of the existing
array.</FONT><BR></P></DIV>
<A NAME="Heading90"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H4 ALIGN="LEFT">
Simple character replacement using the STL replace( ) algorithm</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You may have been hunting through this
chapter trying to do something relatively simple like replace all the instances
of one character with a different character. Upon finding the above section on
replacing, you thought you found the answer but then you started seeing groups
of characters and counts and other things that looked a bit too complex.
Doesn’t <B>string</B> have a way to just replace one character with
another everywhere?</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>string</B> class by itself
doesn’t solve all possible problems. The remainder are relegated to the
STL algorithms, because the <B>string</B> class can look just like an STL
container (the STL algorithms work with anything that looks like an STL
container). All the STL algorithms work on a “range” of elements
within a container. Usually that range is just “from the beginning of the
container to the end.” A <B>string</B> object looks like a container of
characters: to get the beginning of the range you use
<B>string::begin( )</B> and to get the end of the range you use
<B>string::end( )</B>. The following example shows the use of the STL
<B>replace( )</B> algorithm to replace all the instances of ‘X’
with ‘Y’:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C04:StringCharReplace.cpp</font>
<font color=#009900>//{L} ../TestSuite/Test</font>
#include <string>
#include <algorithm>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
string s(<font color=#004488>"aaaXaaaXXaaXXXaXXXXaaa"</font>);
cout << s << endl;
replace(s.begin(), s.end(), 'X', 'Y');
cout << s << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice that this <B>replace( )</B>
is <I>not</I> called as a member function of <B>string</B>. Also, unlike the
<B>string::replace( )</B> functions which only perform one replacement, the
STL replace is replacing all instances of one character with
another.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The STL <B>replace( )</B> algorithm
only works with single objects (in this case, <B>char</B> objects), and will not
perform replacements of quoted <B>char</B> arrays or of <B>string</B>
objects.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since a <B>string</B> looks like an STL
container, there are a number of other STL algorithms that can be applied to it,
which may solve other problems you have that are not directly addressed by the
<B>string</B> member functions. See Chapter XX for more information on the STL
algorithms.</FONT><A NAME="_Toc519041930"></A><BR></P></DIV>
<A NAME="Heading91"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H3 ALIGN="LEFT">
Concatenation using non-member overloaded operators</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the most delightful discoveries
awaiting a C programmer learning about C++ <B>string</B> handling is how simply
<B>string</B>s can be combined and appended using <B>operator+</B> and
<B>operator+=</B>.<B> </B>These operators make combining <B>string</B>s
syntactically equivalent to adding numeric data.</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C04:AddStrings.cpp</font>
<font color=#009900>//{L} ../TestSuite/Test</font>
#include <string>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
string s1(<font color=#004488>"This "</font>);
string s2(<font color=#004488>"That "</font>);
string s3(<font color=#004488>"The other "</font>);
<font color=#009900>// operator+ concatenates strings</font>
s1 = s1 + s2;
cout << s1 << endl;
<font color=#009900>// Another way to concatenates strings</font>
s1 += s3;
cout << s1 << endl;
<font color=#009900>// You can index the string on the right</font>
s1 += s3 + s3[4] + <font color=#004488>"oh lala"</font>;
cout << s1 << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The output looks like
this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>This
This That
This That The other
This That The other ooh lala</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>operator+</B> and <B>operator+=
</B>are a very flexible and<B> </B>convenient means of combining <B>string</B>
data. On the right hand side of the statement, you can use almost any type that
evaluates to a group of one or more
characters.</FONT><A NAME="_Toc519041931"></A><BR></P></DIV>
<A NAME="Heading92"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
Searching in strings</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>find</B> family of <B>string</B>
member functions allows you to locate a character or group of characters within
a given string. Here are the members of the <B>find</B> family and their general
usage:</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana"><B>string find member
function</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana"><B>What/how it finds
</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B> find( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Searches a string for a specified
character or group of characters and returns the starting position of the first
occurrence found or <B>npos</B> if no match is found. (<B>npos </B>is a const of
–1 and indicates that a search failed.) </FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>
find_first_of( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Searches a target string and returns the
position of the first match of <I>any</I> character in a specified group. If no
match is found, it returns <B>npos</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>
find_last_of( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Searches a target string and returns the
position of the last match of <I>any</I> character in a specified group. If no
match is found, it returns <B>npos</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>
find_first_not_of( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Searches a target string and returns the
position of the first element that <I>doesn’t</I> match <I>any</I>
character in a specified group. If no such element is found, it returns
<B>npos</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>
find_last_not_of( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Searches a target string and returns the
position of the element with the largest subscript that <I>doesn’t</I>
match of <I>any</I> character in a specified group. If no such element is found,
it returns <B>npos</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B> rfind( )</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Searches a string from end to beginning
for a specified character or group of characters and returns the starting
position of the match if one is found. If no match is found, it returns
<B>npos</B>.</FONT><BR></P></DIV>
</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -