数组(三)
字符串类
#include <iostream>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
class String
{
public:
// constructors
String();
String(const char* const);
String(const String&);
![](/Images/OutliningIndicators/InBlock.gif)
~String();
![](/Images/OutliningIndicators/InBlock.gif)
// overloaded operators
char& operator [](unsigned short offset);
char operator [](unsigned short offset) const;
String operator +(const String&);
void operator += (const String&);
String& operator = (const String&);
![](/Images/OutliningIndicators/InBlock.gif)
// general accessors
unsigned short GetLen() const { return itsLen; }
const char* GetString() const { return itsString; }
![](/Images/OutliningIndicators/InBlock.gif)
private:
String(unsigned short); // private constructor
char* itsString;
unsigned short itsLen;
};
![](/Images/OutliningIndicators/None.gif)
////////////////////////////////////////////////////////////////////
// 构造函数
![](/Images/OutliningIndicators/None.gif)
String::String() {
itsString = new char[1];
itsString[0] = '\0';
itsLen = 0;
}
![](/Images/OutliningIndicators/None.gif)
String::String(unsigned short len) {
itsString = new char[len + 1];
![](/Images/OutliningIndicators/InBlock.gif)
for (unsigned short i = 0; i <= len; i++)
itsString[i] = '\0';
itsLen = len;
}
![](/Images/OutliningIndicators/None.gif)
String::String(const char* const cString) {
itsLen = strlen(cString);
itsString = new char[itsLen + 1];
![](/Images/OutliningIndicators/InBlock.gif)
for (unsigned short i = 0; i < itsLen; i++)
itsString[i] = cString[i];
itsString[itsLen] = '\0';
}
![](/Images/OutliningIndicators/None.gif)
String::String(const String& rhs) {
itsLen = rhs.GetLen();
itsString = new char[itsLen + 1];
![](/Images/OutliningIndicators/InBlock.gif)
for (unsigned short i = 0; i < itsLen; i++)
itsString[i] = rhs[i]; // 这里的访问操作符在后面要实现之。
itsString[itsLen] = '\0';
}
![](/Images/OutliningIndicators/None.gif)
String::~String() {
delete [] itsString;
itsLen = 0;
}
![](/Images/OutliningIndicators/None.gif)
///////////////////////////////////////////////////////////////////////
// 以下实现自定义的操作符重载
![](/Images/OutliningIndicators/None.gif)
String& String::operator = (const String& rhs) {
// 重要的判断!
if (this == &rhs)
return *this;
delete [] itsString;
itsLen = rhs.GetLen();
itsString = new char[itsLen + 1];
![](/Images/OutliningIndicators/InBlock.gif)
for (unsigned short i = 0; i < itsLen; i++)
itsString[i] = rhs[i];
itsString[itsLen] = '\0';
![](/Images/OutliningIndicators/InBlock.gif)
return *this;
}
![](/Images/OutliningIndicators/None.gif)
char& String::operator [](unsigned short offset) {
if (offset > itsLen)
return itsString[itsLen - 1];
else
return itsString[offset];
}
![](/Images/OutliningIndicators/None.gif)
char String::operator [](unsigned short offset) const {
if (offset > itsLen)
return itsString[itsLen - 1];
else
return itsString[offset];
}
![](/Images/OutliningIndicators/None.gif)
String String::operator +(const String& rhs) {
unsigned short totalLen = itsLen + rhs.GetLen();
String temp = String(totalLen);
![](/Images/OutliningIndicators/InBlock.gif)
unsigned short i, j;
for (i = 0; i < itsLen; i++)
temp[i] = itsString[i];
for (j = 0; j < rhs.GetLen(); i++, j++)
temp[i] = rhs[j];
temp[totalLen] = '\0';
![](/Images/OutliningIndicators/InBlock.gif)
return temp;
}
![](/Images/OutliningIndicators/None.gif)
void String::operator += (const String& rhs) {
unsigned short rhsLen = rhs.GetLen();
unsigned short totalLen = itsLen + rhsLen;
String temp(totalLen);
![](/Images/OutliningIndicators/InBlock.gif)
unsigned short i, j;
for (i = 0; i < itsLen; i++)
temp[i] = itsString[i];
for (j = 0; j < rhsLen; i++, j++)
temp[i] = rhs[j];
temp[totalLen] = '\0';
![](/Images/OutliningIndicators/InBlock.gif)
*this = temp;
}
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
{
String s1("initial test");
cout << "s1: \t" << s1.GetString() << endl;
![](/Images/OutliningIndicators/InBlock.gif)
char* temp = "Hello World";
s1 = temp;
cout << "s1: \t" << s1.GetString() << endl;
![](/Images/OutliningIndicators/InBlock.gif)
char tempTwo[20];
strcpy(tempTwo, "; nice to be here!");
s1 += tempTwo;
cout << "tempTwo: \t" << tempTwo << endl;
cout << "s1: \t" << s1.GetString() << endl;
![](/Images/OutliningIndicators/InBlock.gif)
cout << "s1[4]: \t" << s1[4] << endl;
s1[4] = 'x';
cout << "s1: \t" << s1.GetString() << endl;
![](/Images/OutliningIndicators/InBlock.gif)
cout << "s1[999]: \t" << s1[999] << endl;
![](/Images/OutliningIndicators/InBlock.gif)
String s2("Another string");
String s3;
s3 = s1 + s2;
cout << "s3: \t" << s3.GetString() << endl;
![](/Images/OutliningIndicators/InBlock.gif)
String s4;
s4 = "Why does this work?";
cout << "s4: \t" << s4.GetString() << endl;
return 0;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)