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

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

📁 linux下c++编程的好文章
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: Linux Tutorial: - C++ String Class Tutorial
Date: Wed, 4 Apr 2007 11:01:50 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
	type="text/html";
	boundary="----=_NextPart_000_014D_01C776A8.A5A86B90"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028

This is a multi-part message in MIME format.

------=_NextPart_000_014D_01C776A8.A5A86B90
Content-Type: text/html;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html

=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Linux Tutorial: - C++ String Class =
Tutorial</TITLE><LINK=20
href=3D"http://www.yolinux.com/TUTORIALS/yolinux.css" type=3Dtext/css=20
rel=3DSTYLESHEET>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<META=20
content=3D"String Class,Standard Template =
Library,class,object,C++,Tutorial,examples,info,linux"=20
name=3DKEYWORDS>
<META=20
content=3D"ANSI C++ GNU String class tutorial and examples. YoLinux: =
Linux Information Portal includes informative tutorials and links to =
many Linux sites."=20
name=3DDESCRIPTION>
<META content=3D"Greg Ippolito" name=3Dauthor>
<META content=3D"MSHTML 6.00.2900.3059" name=3DGENERATOR></HEAD>
<BODY text=3D#000000 bgColor=3D#cccccc>
<HR SIZE=3D5>

<H1>YoLinux Tutorial: C++ String class Examples and Tutorial</H1>
<P>
<TABLE>
  <TBODY>
  <TR>
    <TD width=3D"70%">C++ may use the C string functions but they rely =
on a null=20
      termination and proper memory allocation to hold the string. The =
ANSI C++=20
      GNU string classes included in the C++ standard library attempt to =

      simplify string manipulation by automating much of the memory =
allocation=20
      and management. Included in this tutorial are examples of the =
string=20
      classes included with the GNU g++ compiler on Linux. This string =
class is=20
      cross platform and included with both Unix/Linux ANSI and =
Microsoft Visual=20
      C++ compilers. Because it is standard with cross platform =
availability,=20
      the "string" class is preferred over the Microsoft MFC "String" =
class.=20
  </TD></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dright><!-- BEGIN RICH-MEDIA BURST! CODE =
-->
      <SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 728;=0A=
google_ad_height =3D 90;=0A=
google_ad_format =3D "728x90_as";=0A=
google_ad_type =3D "text_image";=0A=
google_ad_channel =3D"";=0A=
google_color_border =3D "CCCCCC";=0A=
google_color_bg =3D "FFFFFF";=0A=
google_color_link =3D "000000";=0A=
google_color_url =3D "666666";=0A=
google_color_text =3D "333333";=0A=
//--></SCRIPT>

      <SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
      type=3Dtext/javascript>=0A=
</SCRIPT>
<!-- END BURST CODE --></TD></TR></TBODY></TABLE>
<P>
<HR SIZE=3D5>

<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" bgColor=3D#000000 =
border=3D1>
  <TBODY>
  <TR bgColor=3D#cccccc>
    <TD vAlign=3Dtop width=3D160 bgColor=3D#c0c0c0>
      <P><FONT size=3D-1><B>Related YoLinux Tutorials:</B>=20
      <P>=C2=B0<A=20
      =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html">C++ =
STL=20
      vector, list</A>=20
      <P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++.html">C++=20
      Info, links</A>=20
      <P>=C2=B0<A=20
      =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialSoftwareDevelopment=
.html">Software=20
      development tools</A>=20
      <P>=C2=B0<A=20
      =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
">C++=20
      Coding Style</A>=20
      <P>=C2=B0<A=20
      =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialXemacs.html">Emacs =
and=20
      C/C++</A>=20
      <P>=C2=B0<A=20
      =
href=3D"http://www.yolinux.com/TUTORIALS/MicrosoftVisualC++Tips.html">MS/=
Visual=20
      C++ tips and best practices</A>=20
      <P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/index.html">YoLinux=20
      Tutorials Index</A> </FONT>
      <P>
      <HR>

      <P>
      <SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 160;=0A=
google_ad_height =3D 600;=0A=
google_ad_format =3D "160x600_as";=0A=
google_ad_channel =3D"";=0A=
google_ad_type =3D "text";=0A=
google_color_border =3D ["336699","000000","6699CC","003366"];=0A=
google_color_bg =3D ["FFFFFF","F0F0F0","003366","003366"];=0A=
google_color_link =3D ["0000FF","0000FF","FFFFFF","FF6600"];=0A=
google_color_url =3D ["008000","008000","AECCEB","99CCFF"];=0A=
google_color_text =3D ["000000","000000","AECCEB","FFFFFF"];=0A=
//--></SCRIPT>

      <SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
      type=3Dtext/javascript>=0A=
</SCRIPT>

      <P>
      <HR>

      <P><A href=3D"http://yolinux.tradepub.com/">Free Information =
Technology=20
      Magazine Subscriptions and Document Downloads</A>=20
      <P>
      <HR>

      <P><A =
href=3D"http://yolinux.tradepub.com/?pt=3Dcat&amp;page=3DInfosoft">Free=20
      Information Technology <B>Software and Development</B> Magazine=20
      Subscriptions and Document Downloads</A>=20
      <P>
      <HR>

      <P></P></TD>
    <TD>
      <P>
      <TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
        <TBODY>
        <TR bgColor=3D#ffcc33>
          <TD><B><BIG>String class =
example:</BIG></B></TD></TR></TBODY></TABLE>
      <P>Simple example of a program using string class:=20
      <P>
      <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;string&gt;
#include &lt;iostream&gt;

using namespace std;

main()
{
   string a("abcd efg");
   string b("xyz ijk");
   string c;

   cout &lt;&lt; a &lt;&lt; " " &lt;&lt; b &lt;&lt; endl;                =
        // Output: abcd efg xyz ijk

   cout &lt;&lt; "String empty: "    &lt;&lt; c.empty()    &lt;&lt; =
endl;  // String empty: 1=20
                                                         // Is string =
empty? Yes it is empty. (TRUE)
   c =3D a + b;                                            // =
concatenation
   cout &lt;&lt; c &lt;&lt; endl;                                    // =
abcd efgxyz ijk
   cout &lt;&lt; "String length: "   &lt;&lt; c.length()   &lt;&lt; =
endl;  // String length: 15
   cout &lt;&lt; "String size: "     &lt;&lt; c.size()     &lt;&lt; =
endl;  // String size: 15
   cout &lt;&lt; "String capacity: " &lt;&lt; c.capacity() &lt;&lt; =
endl;  // String capacity: 15
   cout &lt;&lt; "String empty: "    &lt;&lt; c.empty()    &lt;&lt; =
endl;  // String empty: 0=20
                                                         // Is string =
empty? No it is NOT empty. (FALSE)
   string d =3D c;
   cout &lt;&lt; d &lt;&lt; endl;                                    // =
abcd efgxyz ijk

                                                         // First =
character: a
   cout &lt;&lt; "First character: " &lt;&lt; c[0] &lt;&lt; endl;        =
  // Strings start with index 0 just like C.

   string f("    Leading and trailing blanks      ");
   cout &lt;&lt; "String f:" &lt;&lt; f &lt;&lt; endl;
   cout &lt;&lt; "String length: " &lt;&lt; f.length() &lt;&lt; endl;    =
  // String length: 37
   cout &lt;&lt; "String f:" &lt;&lt; f.append("ZZZ") &lt;&lt; endl;     =
  // String f:    Leading and trailing blanks      ZZZ
   cout &lt;&lt; "String length: " &lt;&lt; f.length() &lt;&lt; endl;    =
  // String length: 40

   string g("abc abc abd abc");
   cout &lt;&lt; "String g: " &lt;&lt; g &lt;&lt; endl;                  =
  // String g: abc abc abd abc
   cout &lt;&lt; "Replace 12,1,\"xyz\",3: " &lt;&lt; =
g.replace(12,1,"xyz",3) &lt;&lt; endl;  // Replace 12,1,"xyz",3: abc abc =
abd xyzbc
   cout &lt;&lt; g.replace(0,3,"xyz",3) &lt;&lt; endl;               // =
xyz abc abd xyzbc
   cout &lt;&lt; g.replace(4,3,"xyz",3) &lt;&lt; endl;               // =
xyz xyz abd xyzbc
   cout &lt;&lt; g.replace(4,3,"ijk",1) &lt;&lt; endl;               // =
xyz i abd xyzbc
   cout &lt;&lt; "Find: " &lt;&lt; g.find("abd",1) &lt;&lt; endl;        =
  // Find: 6
   cout &lt;&lt; g.find("qrs",1) &lt;&lt; endl;

   string h("abc abc abd abc");
   cout &lt;&lt; "String h: " &lt;&lt; h &lt;&lt; endl;
   cout &lt;&lt; "Find \"abc\",0: " &lt;&lt; h.find("abc",0) &lt;&lt; =
endl; // Find "abc",0: 0
   cout &lt;&lt; "Find \"abc\",1: " &lt;&lt; h.find("abc",1) &lt;&lt; =
endl; // Find "abc",1: 4
   cout &lt;&lt; "Find_first_of \"abc\",0: " &lt;&lt; =
h.find_first_of("abc",0) &lt;&lt; endl; // Find_first_of "abc",0: 0
   cout &lt;&lt; "Find_last_of \"abc\",0: " &lt;&lt; =
h.find_last_of("abc",0) &lt;&lt; endl;   // Find_last_of "abc",0: 0
   cout &lt;&lt; "Find_first_not_of \"abc\",0: " &lt;&lt; =
h.find_first_not_of("abc",0) &lt;&lt; endl;  // Find_first_not_of =
"abc",0: 3
   cout &lt;&lt; "Find_first_not_of \" \": " &lt;&lt; =
h.find_first_not_of(" ") &lt;&lt; endl;  // Find_first_not_of " ": 0
   cout &lt;&lt; "Substr 5,9: " &lt;&lt; h.substr(5,9) &lt;&lt; endl;    =
   // Substr 5,9: bc abd ab
   cout &lt;&lt; "Compare 0,3,\"abc\": " &lt;&lt; h.compare(0,3,"abc") =
&lt;&lt; endl;  // Compare 0,3,"abc": 0
   cout &lt;&lt; "Compare 0,3,\"abd\": " &lt;&lt; h.compare(0,3,"abd") =
&lt;&lt; endl;  // Compare 0,3,"abd": -1
   cout &lt;&lt; h.assign("xyz",0,3) &lt;&lt; endl;                   // =
xyz
   cout &lt;&lt; "First character: " &lt;&lt; h[0] &lt;&lt; endl; // =
Strings start with 0 // First character: x


}

</PRE></TD></TR></TBODY></TABLE></DD></DL>
      <P>
      <HR>

      <P>Compile: <TT>g++ program.cpp</TT>=20
      <P><FONT color=3D#ff0000>[Potential Pitfall]</FONT>: In Red Hat =
Linux=20
      versions 7.x one could omit the "<TT>using namespace std;</TT>" =
statement.=20
      Use of this statement is good programming practice and is required =
in Red=20
      Hat 8.0.=20
      <P><FONT color=3D#ff0000>[Potential Pitfall]</FONT>: Red Hat 8.0 =
requires=20
      the reference to "<TT>#include &lt;iostream&gt;</TT>". Red Hat =
versions=20
      7.x used "<TT>#include &lt;iostream.h&gt;</TT>". (Also fstream, =
...)=20
      <P>
      <HR>

      <P>
      <TABLE width=3D"100%">
        <TBODY>
        <TR bgColor=3D#c0c0c0>
          <TD>
          <TD vAlign=3Dtop>Output: <TT>./a.out</TT>=20
            <DL>
              <DD><PRE>abcd efg xyz ijk
String empty: 1
abcd efgxyz ijk
String length: 15

⌨️ 快捷键说明

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