⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux tutorial - c++ string class tutorial.mht

📁 linux下c++编程的好文章
💻 MHT
📖 第 1 页 / 共 5 页
字号:
String size: 15
String capacity: 15
String empty: 0
abcd efgxyz ijk
First character: a
String f:    Leading and trailing blanks     =20
String length: 37
String f:    Leading and trailing blanks      ZZZ
String length: 40
String g: abc abc abd abc
Replace 12,1,"xyz",3: abc abc abd xyzbc
xyz abc abd xyzbc
xyz xyz abd xyzbc
xyz i abd xyzbc
Find: 6
4294967295
String h: abc abc abd abc
Find "abc",0: 0
Find "abc",1: 4
Find_first_of "abc",0: 0
Find_last_of "abc",0: 0
Find_first_not_of "abc",0: 3
Find_first_not_of " ": 0
Substr 5,9: bc abd ab
Compare 0,3,"abc": 0
Compare 0,3,"abd": -1
xyz
First character: x

</PRE></DD></DL></TD>
          <TD vAlign=3Dtop align=3Dright>
            <SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 300;=0A=
google_ad_height =3D 250;=0A=
google_ad_format =3D "300x250_as";=0A=
google_ad_channel =3D"";=0A=
google_color_border =3D ["6699CC","003366","2D5893","333333"];=0A=
google_color_bg =3D ["003366","000000","99AACC","000000"];=0A=
google_color_link =3D ["FFFFFF","FFFFFF","000000","FFFFFF"];=0A=
google_color_url =3D ["AECCEB","FF6600","000099","999999"];=0A=
google_color_text =3D ["AECCEB","FF6600","003366","CCCCCC"];=0A=
//--></SCRIPT>

            <SCRIPT=20
            =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
            type=3Dtext/javascript>=0A=
</SCRIPT>
          </TD></TR></TBODY></TABLE><FONT color=3D#ff0000>[Potential =
Pitfall]</FONT>:=20
      There have been some changes in the behavior of the string class =
from Red=20
      Hat 7.x to Red Hat 8.0:=20
      <UL>
        <LI>The compare function arguments have changed from=20
        <TT>X.compare("string",int-1, int-2);</TT> to =
<TT>X.compare(int-1,=20
        int-2, "string");</TT>=20
        <LI>The return value of the compare function call=20
        <TT>h.compare("abc",0,3)</TT> in 7.x was 12. In Red Hat 8.0=20
        <TT>h.compare(0,3,"abc")</TT> it is 0.=20
        <LI>String capacity function call "<TT>c.capacity()</TT>" is 15. =
The=20
        returned value in Red Hat 7.x was 16. </LI></UL>
      <DL></DL>
      <P>
      <HR SIZE=3D5>

      <TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
        <TBODY>
        <TR bgColor=3D#ffcc33>
          <TD><B><BIG>String class =
functions:</BIG></B></TD></TR></TBODY></TABLE>
      <P>
      <UL>
        <LI>Constructors: <PRE>    string sVar1("abc");
    string sVar1(<I>C-string</I>);
    string sVar2(10," ");  // Generate string initialized to 10 blanks.
    string sVar3(Var1,<I>string-index</I>);  // Initialize with =
characters from string starting with index <I>string-index</I>.
    string sVar4(<I>iterator-index-begin, iterator-index-end</I>)
    </PRE>
        <LI>Destructor: <PRE>    Var.~string();         // Destructor
    </PRE>
        <LI>Replace:=20
        <UL>
          =
<LI>Var.replace(<I>beginning,end-position,string-class-variable</I>)=20
          <LI>Var.replace(<I>beginning,end-position,C-char-variable</I>) =

          =
<LI>Var.replace(<I>beginning,end-position,string-class-variable,length</I=
>)=20

          =
<LI>Var.replace(<I>beginning,end-position,integer-number,single-char</I>)=
=20

          =
<LI>Var.replace(<I>beginning,end-position,new-beginning-porition,new-end-=
position</I>)=20
          </LI></UL>
        <P>Code samples: <PRE>       string g("abc abc abd abc");
       cout &lt;&lt; g.replace(4,1,"ijk",3) &lt;&lt; endl;

       string h("abc abc abd abc");
       cout &lt;&lt; h.replace(4,6,"ijk",3) &lt;&lt; endl;

       string k("abc abc abd abc");
       cout &lt;&lt; k.replace(4,3,"ijk",3) &lt;&lt; endl;
=20
       string l("abc abc abd abc");
       cout &lt;&lt; k.replace(12,1,"xyz",3) &lt;&lt; endl;
    </PRE>Output: <PRE>    abc ijkbc abd abc       <B><I>- Beginning =
with the 4th index (character number 5) replace one character with 3 =
characters from string "ijk"</I></B>
    abc ijkd abc
    abc ijk abd abc
    abc abc abd xyzbc
    </PRE>
        <LI>Find: (also rfind(), find_first_of(), find_last_of(),=20
        find_first_not_of(), find_last_not_of())=20
        <P>Arguments/parameters:=20
        <UL>
          <LI><TT>Val.find(const string&amp; <I>argument</I>) =
</TT><BR>Find=20
          first occurence of <I>argument</I> within <TT>string Val</TT>=20
          <LI>find(const string&amp; <I>argument</I>, size_type =
<I>index</I>)=20
          <BR>Find first occurence of <I>argument</I> within <TT>string =
Val</TT>=20
          starting search from position <I>index</I>.=20
          <LI>find(const char* <I>argument</I>)=20
          <LI>find(const char* <I>argument</I>, size_type <I>index</I>)=20
          <LI>find(const char* <I>argument</I>, size_type <I>index</I>,=20
          size_type <I>length</I>) <BR>Find first occurence of =
<I>argument</I>=20
          within <TT>string Val</TT> starting search from position =
<I>index</I>=20
          and search for <I>length</I> number of characters. =
</LI></UL></LI></UL>
      <P>
      <HR>

      <P>
      <H4>List of operations:</H4>Assuming declaration: <TT>string =
Var;</TT>=20
      <DL>
        <DD>
        <TABLE border=3D1>
          <TBODY>
          <TR bgColor=3Dsilver>
            <TH>Function/Operation</TH>
            <TH>Description</TH></TR>
          <TR>
            <TD>Var =3D=20
            <I>string2</I><BR>Var.assign("<I>string-to-assign</I>")</TD>
            <TD>Assignment of value to string. When assigning a C "char" =
data=20
              type, first check if NULL to avoid failure/crash.<BR>i.e.: =
<TT>if(=20
              szVar ) sVar.assign( szVar );</TT><BR>where szVar is a C =
"char *"=20
              data type and sVar is of type "string".</TD></TR>
          <TR>
            =
<TD>Var.swap(<I>string2</I>)<BR>swap(<I>string1,string2</I>)</TD>
            <TD>Swap with value held in string2.<BR>Function swap will=20
              exchange contents of two string class variables.</TD></TR>
          <TR>
            <TD>Var +=3D =
<I>string2</I><BR>Var.append()<BR>Var.push_back()</TD>
            <TD>Append string/characters.</TD></TR>
          <TR>
            <TD>Var.insert()</TD>
            <TD>Insert characters</TD></TR>
          <TR>
            <TD>Var.erase()<BR>Var =3D ""</TD>
            <TD>Clear string variable. No arguments necessary.</TD></TR>
          <TR>
            <TD>+</TD>
            <TD>Concatenate</TD></TR>
          <TR>
            <TD>=3D=3D, !=3D, &lt;, &lt;=3D, &gt;,=20
            &gt;=3D<BR>Var.compare(<I>string</I>)</TD>
            <TD>Compare strings.</TD></TR>
          <TR>
            <TD>Var.length()</TD>
            <TD>Return length of string. No arguments necessary. The =
methods=20
              <TT>length(), size()</TT> and <TT>capacity()</TT> all =
return the=20
              same value.</TD></TR>
          <TR>
            <TD>Var.size()</TD>
            <TD>Return length of string. No arguments =
necessary.</TD></TR>
          <TR>
            <TD>Var.capacity()</TD>
            <TD>Return length of string + 1. Red Hat 7.x. Red Hat 8.0+ =
returns=20
              the number of characters without the "+1". Number of =
characters=20
              that can be held without re-allocation. <BR>No arguments=20
            necessary.</TD></TR>
          <TR>
            <TD>Var.max_size()</TD>
            <TD>Returns a very large number. No arguments =
necessary.</TD></TR>
          <TR>
            <TD>Var.empty()</TD>
            <TD>Returns 1 if an empty string.<BR>Returns 0 if not =
empty.</TD></TR>
          <TR>
            <TD>&lt;&lt;</TD>
            <TD>Output stream</TD></TR>
          <TR>
            <TD>&gt;&gt;<BR>getline()</TD>
            <TD>Input stream</TD></TR>
          <TR>
            <TD>Var.c_str()</TD>
            <TD>Returns C string pointer. C char string is null =
terminated. Do=20
              not free memory using this pointer!</TD></TR>
          <TR>
            <TD>Var.data()</TD>
            <TD>Returns C string pointer. C char string is NOT null=20
              terminated. Do not free memory using this =
pointer!</TD></TR>
          <TR>
            <TD>Var[]<BR>Var.at(<I>integer</I>)</TD>
            <TD>Access individual characters.</TD></TR>
          <TR>
            <TD>Var.find()<BR>Var.rfind()</TD>
            <TD>Find first occurance of string or substring.<BR>Find =
last=20
              occurance of string or substring.</TD></TR>
          <TR>
            <TD>Var.find_first_of()<BR>Var.find_last_of()</TD>
            <TD>Find strings and substrings.</TD></TR>
          <TR>
            <TD>Var.find_first_not_of()<BR>Var.find_last_not_of()</TD>
            <TD>Find strings and substrings.</TD></TR>
          <TR>
            <TD>Var.replace()</TD>
            <TD>Replace section of string with new characters.</TD></TR>
          <TR>
            <TD>Var.substr()</TD>
            <TD>Return substring of text</TD></TR>
          <TR>
            <TD>Var.begin()<BR>Var.end()</TD>
            <TD>Iterators</TD></TR>
          <TR>
            <TD>Var.rbegin()<BR>Var.rend()</TD>
            <TD>Reverse iterators</TD></TR></TBODY></TABLE></DD></DL>
      <P>Note that in most cases the string functions have been =
overloaded to=20
      accept both string class arguments and C char variables.=20
      <P>
      <HR SIZE=3D5>

      <TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
        <TBODY>
        <TR bgColor=3D#ffcc33>
          <TD><B><BIG>ANSI C++ string class=20
      iterators:</BIG></B></TD></TR></TBODY></TABLE>
      <P>Iterators provide the ability to access the individual =
characters in a=20
      string.=20
      <DL>
        <DD>
        <TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
          <TBODY>
          <TR bgColor=3D#c0c0c0>
            <TD><PRE>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;
  =20
int main()
{
   string alphabetLC=3D"abcdefghijklmnopqrstuvwxyz";

   string::const_iterator cii;
   int ii;

   for(cii=3DalphabetLC.begin(); cii!=3DalphabetLC.end(); cii++)
   {
      cout &lt;&lt; ii++ &lt;&lt; " " &lt;&lt; *cii &lt;&lt; endl;
   }
}
</PRE></TD></TR></TBODY></TABLE>This will print the integer position in=20
        the string followed by the letter for all characters in the =
alphabet.=20
        <TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
          <TBODY>
          <TR bgColor=3D#c0c0c0>
            <TD><PRE>0 a
1 b
2 c
3 d
4 e

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -