📄 linux tutorial - c++ string class tutorial.mht
字号:
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&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 <string>
#include <iostream>
using namespace std;
main()
{
string a("abcd efg");
string b("xyz ijk");
string c;
cout << a << " " << b << endl; =
// Output: abcd efg xyz ijk
cout << "String empty: " << c.empty() << =
endl; // String empty: 1=20
// Is string =
empty? Yes it is empty. (TRUE)
c =3D a + b; // =
concatenation
cout << c << endl; // =
abcd efgxyz ijk
cout << "String length: " << c.length() << =
endl; // String length: 15
cout << "String size: " << c.size() << =
endl; // String size: 15
cout << "String capacity: " << c.capacity() << =
endl; // String capacity: 15
cout << "String empty: " << c.empty() << =
endl; // String empty: 0=20
// Is string =
empty? No it is NOT empty. (FALSE)
string d =3D c;
cout << d << endl; // =
abcd efgxyz ijk
// First =
character: a
cout << "First character: " << c[0] << endl; =
// Strings start with index 0 just like C.
string f(" Leading and trailing blanks ");
cout << "String f:" << f << endl;
cout << "String length: " << f.length() << endl; =
// String length: 37
cout << "String f:" << f.append("ZZZ") << endl; =
// String f: Leading and trailing blanks ZZZ
cout << "String length: " << f.length() << endl; =
// String length: 40
string g("abc abc abd abc");
cout << "String g: " << g << endl; =
// String g: abc abc abd abc
cout << "Replace 12,1,\"xyz\",3: " << =
g.replace(12,1,"xyz",3) << endl; // Replace 12,1,"xyz",3: abc abc =
abd xyzbc
cout << g.replace(0,3,"xyz",3) << endl; // =
xyz abc abd xyzbc
cout << g.replace(4,3,"xyz",3) << endl; // =
xyz xyz abd xyzbc
cout << g.replace(4,3,"ijk",1) << endl; // =
xyz i abd xyzbc
cout << "Find: " << g.find("abd",1) << endl; =
// Find: 6
cout << g.find("qrs",1) << endl;
string h("abc abc abd abc");
cout << "String h: " << h << endl;
cout << "Find \"abc\",0: " << h.find("abc",0) << =
endl; // Find "abc",0: 0
cout << "Find \"abc\",1: " << h.find("abc",1) << =
endl; // Find "abc",1: 4
cout << "Find_first_of \"abc\",0: " << =
h.find_first_of("abc",0) << endl; // Find_first_of "abc",0: 0
cout << "Find_last_of \"abc\",0: " << =
h.find_last_of("abc",0) << endl; // Find_last_of "abc",0: 0
cout << "Find_first_not_of \"abc\",0: " << =
h.find_first_not_of("abc",0) << endl; // Find_first_not_of =
"abc",0: 3
cout << "Find_first_not_of \" \": " << =
h.find_first_not_of(" ") << endl; // Find_first_not_of " ": 0
cout << "Substr 5,9: " << h.substr(5,9) << endl; =
// Substr 5,9: bc abd ab
cout << "Compare 0,3,\"abc\": " << h.compare(0,3,"abc") =
<< endl; // Compare 0,3,"abc": 0
cout << "Compare 0,3,\"abd\": " << h.compare(0,3,"abd") =
<< endl; // Compare 0,3,"abd": -1
cout << h.assign("xyz",0,3) << endl; // =
xyz
cout << "First character: " << h[0] << 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 <iostream></TT>". Red Hat =
versions=20
7.x used "<TT>#include <iostream.h></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 + -