📄 linux tutorial - c++ string class tutorial.mht
字号:
5 f
6 g
7 h
...
..
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<P>
<HR>
<P>
<H4>Iterator types:</H4>
<UL>
<LI>string::traits_type=20
<LI>string::value_type=20
<LI>string::size_type=20
<LI>string::difference_type=20
<LI>string::reference=20
<LI>string::const_reference=20
<LI>string::pointer=20
<LI>string::const_pointer=20
<LI>string::iterator=20
<LI>string::const_iterator=20
<LI>string::reverse_iterator=20
<LI>string::const_reverse_iterator=20
<LI>string::npos </LI></UL>
<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 and the C standard=20
library:</BIG></B></TD></TR></TBODY></TABLE>
<P>The full use of the C standard library is available for use by=20
utilizing the ".c_str" function return of the 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 <strings.h>
#include <string>
#include <stdio.h>
using namespace std;
=20
int main()
{
char *phrase1=3D"phrase";
string phrase2("Second phrase");
char phraseA[128];
char *phraseB;
=20
strcpy(phraseA,phrase2.c_str());=20
phraseB =3D strstr(phrase2.c_str(),phrase1);
printf("phraseA: %s\n",phraseA);
printf("phraseB: %s\n",phraseB);
printf("phrase2: %s\n",phrase2.c_str());
} =20
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<P>Compile and run:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE><B>[prompt]$</B> g++ test.cpp
<B>[prompt]$</B> ./a.out
phraseA: Second phrase
phraseB: phrase
phrase2: Second phrase
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<P>
<HR SIZE=3D5>
<TABLE>
<TBODY>
<TR>
<TD vAlign=3Dtop width=3D"50%">
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" =
border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Using ostringstream and an internal=20
write:</BIG></B></TD></TR></TBODY></TABLE>
<P>In memory I/O string processing used as a data type =
conversion.=20
This can also be used to make use of formatting of output in =
memory.=20
<P>File: <TT>int2string.cpp</TT>=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 <iostream>
#include <sstream>
#include <string>
using namespace std;
=20
string int2string(const int& number)
{
ostringstream oss;
oss << number;
return oss.str();
}
=20
main()
{
int number=3D7878;
string test=3D"SSSSS";
test +=3D int2string(number);
cout << test << endl;
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>Compile and run:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE> <B>[prompt]$</B> g++ int2string.cpp
<B>[prompt]$</B> a.out
SSSSS7878
</PRE></TD></TR></TBODY></TABLE></DD></DL><FONT=20
color=3D#ff0000>[Potential Pitfall]</FONT>: Returned string =
value must=20
be used right away without other memory being set as string=20
destructor will free the memory associated with its =
contents. It is=20
much safer for the function to return a <TT>char</TT> data =
type or=20
pass the string reference as an argument. </TD>
<TD vAlign=3Dtop width=3D"50%">
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" =
border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Using istringstream and an internal=20
read:</BIG></B></TD></TR></TBODY></TABLE>
<P>This is used to make use of reading and parsing a string =
in=20
memory. It will also allow data type conversion from a =
string to the=20
type read.=20
<P>File: <TT>test.cpp</TT>=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 <iostream>
#include <sstream>
#include <string>
using namespace std;
=20
main()
{
string test=3D"AAA 123 SSSSS 3.141592654";
istringstream totalSString( test );
string string1, string2;
int integer1;
double PI;
totalSString >> string1 >> integer1 >> string2 =
>> PI;
=20
cout << "Individual parsed variables:" << endl;
cout << "First string: " << string1 << endl;
cout << "First integer: " << integer1 << endl;
cout << "Value of PI: " << PI << endl;
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>Compile and run:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE> <B>[prompt]$</B> g++ test.cpp
<B>[prompt]$</B> a.out
Individual parsed variables:
First string: AAA
First integer: 123
Value of PI: 3.14159
</PRE></TD></TR></TBODY></TABLE></DD></DL></TD></TR></TBODY></TABLE>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Code snipets:</BIG></B></TD></TR></TBODY></TABLE>
<P>
<UL>
<LI><B>Read lines from standard input:</B>=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE> while( getline(std::cin, sLine) )
{
if( sLine.empty() ); // Ignore empty lines
else
{
cout << sLine[0] << sLine[1] << endl;
....
...
}
}
</PRE></TD></TR></TBODY></TABLE>
<LI><B>Read lines from input file:</B>=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE> #define SYS_CONFIG_FILE "/etc/file.conf"
#include <string>
#include <algorithm>
#include <vector>
#include <cctype>
#include <iostream>
#include <fstream>
using namespace std;
string::size_type posBeginIdx, posEndIdx;
string::size_type ipos=3D0;
string sLine, sValue;
string sKeyWord;
const string sDelim( ":" );
ifstream myInputFile(SYS_CONFIG_FILE, ios::in);
if( !myInputFile )
{
sError =3D "File SYS_CONFIG_FILE could not be opened";
return sError; // ERROR
}
while( getline(myInputFile,sLine) )
{
if( sLine.empty() ); // Ignore empty lines
else
{
posEndIdx =3D sLine.find_first_of( sDelim );
sKeyWord =3D sLine.substr( ipos, posEndIdx ); // Extract word
posBeginIdx =3D posEndIdx + 1; // Beginning of next word =
(after ':')
....
...
}
}
</PRE></TD></TR></TBODY></TABLE>
<LI><B>Strip blank characters:</B>=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>void
stripLeadingAndTrailingBlanks(string& StringToModify)
{
if(StringToModify.empty()) return;
int startIndex =3D StringToModify.find_first_not_of(" ");
int endIndex =3D StringToModify.find_last_not_of(" ");
string tempString =3D StringToModify;
StringToModify.erase();
StringToModify =3D tempString.substr(startIndex, =
(endIndex-startIndex+ 1) );
}
</PRE></TD></TR></TBODY></TABLE></LI></UL>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>The String Class and Debugging in=20
GDB:</BIG></B></TD></TR></TBODY></TABLE>
<P>The first thing you will notice when using the C++ string class =
is that=20
you can't de-reference any of the string class variables directly =
with=20
GDB, ddd,... One must create a helper routine (for older versions =
of gdb)=20
or use string class funtions (newer versions of gdb) to print out =
the=20
value of the string variable.=20
<DL>
<DD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -