和C#不一样,C和C++的内部都没有字符串数据类型,但是我们可以用C++建立一个实现字符串相关操作的类型:String
并且利用重载的机制提供方便操作字符串的相关运算符集。
下面的程序分成两个部分:
(1)String类:类头String.h和类实现String.cpp
(2)String类使用演示程序Main.cpp
类头文件String.h代码如下:
- #ifndef STRING_H
- #define STRING_H
- #include <iostream>
- using namespace std;
-
- class String
- {
- friend ostream & operator<<(ostream & output, const String & s);
- friend istream & operator>>(istream & input, String & s);
-
- public:
- String(const char* = "");
- String(const String &);
- ~String();
- const String & operator=(const String &);
- String & operator+=(const String &);
- int operator!() const;
- int operator==(const String &) const;
- int operator!=(const String &) const;
- int operator<(const String &) const;
- int operator>(const String &) const;
- int operator<=(const String &) const;
- int operator>=(const String &) const;
- char & operator[](int);
- String & operator()(int, int);
- int GetLength() const;
- private:
- char* sPtr;
- int length;
- };
-
- #endif
类实现文件String.cpp代码如下:
- #include <iostream>
- #include <string.h>
- #include <assert.h>
- #include "String.h"
-
- ostream & operator<<(ostream & output, const String & s)
- {
- output<<s.sPtr;
- return output;
- }
-
- istream & operator>>(istream & input, String & s)
- {
- static char temp[100];
- input>>temp;
- s = temp;
- return input;
- }
-
- String::String(const char * s)
- {
- cout<<"Convention constructor:"<<s<<endl;
- length = strlen(s);
- sPtr = new char[length+1];
- assert(sPtr!=0);
- strcpy(sPtr, s);
- }
-
- String::String(const String & copy)
- {
- cout<<"Copy constructor:"<<copy.sPtr<<endl;
- length = copy.length;
- sPtr = new char[length+1];
- assert(sPtr != 0);
- strcpy(sPtr, copy.sPtr);
- }
-
- String::~String()
- {
- cout<<"Destructor:"<<sPtr<<endl;
- delete[] sPtr;
- }
-
- const String & String::operator=(const String & right)
- {
- cout<<"operator = called"<<endl;
- if(&right != this)
- {
- delete[] sPtr;
- length = right.length;
- sPtr = new char[length+1];
- assert(sPtr!=0);
- strcpy(sPtr, right.sPtr);
- }
- else
- {
- cout<<"Attempt assignment of a String to itself/n";
- }
- return *this;
- }
-
- String & String::operator+=(const String & right)
- {
- char* tempPtr = sPtr;
- length += right.length;
- sPtr = new char[length+1];
- assert(sPtr!=0);
- strcpy(sPtr, tempPtr);
- strcat(sPtr, right.sPtr);
- delete[] tempPtr;
- return *this;
- }
-
-
- int String::operator !() const
- {
- return length == 0;
- }
-
- int String::operator==(const String & right) const
- {
- return strcmp(sPtr, right.sPtr)==0;
- }
-
- int String::operator!=(const String & right) const
- {
- return strcmp(sPtr, right.sPtr)!=0;
- }
-
- int String::operator<(const String & right) const
- {
- return strcmp(sPtr, right.sPtr)<0;
- }
-
- int String::operator>(const String & right) const
- {
- return strcmp(sPtr, right.sPtr)>0;
- }
-
- int String::operator<=(const String & right) const
- {
- return strcmp(sPtr, right.sPtr)<=0;
- }
-
- int String::operator>=(const String & right) const
- {
- return strcmp(sPtr, right.sPtr)>=0;
- }
-
-
- char & String::operator [](int subscript)
- {
-
- assert(subscript>=0 && subscript<length);
- return sPtr[subscript];
- }
-
- String & String::operator()(int index, int sublength)
- {
-
- assert(index>=0 && index<length && sublength >= 0);
-
- String * subPtr = new String;
- assert(subPtr!=0);
-
-
- int size = 0;
- if(sublength == 0 || (index+sublength>length))
- {
- size = length - index + 1;
- }
- else
- {
- size = sublength + 1;
- }
-
- delete subPtr->sPtr;
- subPtr->length = size;
- subPtr->sPtr = new char[size];
- assert(subPtr->sPtr!=0);
-
- int i = index, j=0;
-
- for( ; i<index+size-1; i++, j++)
- {
- subPtr->sPtr[j] = sPtr[i];
- }
-
- subPtr->sPtr[j] = '/0';
- return *subPtr;
- }
-
- int String::GetLength() const
- {
- return length;
- }
String类使用演示程序Main.cpp代码如下:
- #include <iostream>
- #include "String.h"
-
- int main()
- {
- cout<<"---------------------------------------------------------------------------------------------------------/n";
- cout<<" 一个有用的ANSI字符串类String的实现及使用演示程序 /n";
- cout<<"by 一剑(Loomman),QQ:28077188,MSN: [email protected] QQ裙:30515563/n";
- cout<<"---------------------------------------------------------------------------------------------------------/n/n";
-
- String s1(" happy"), s2(" birthday"), s3;
-
- cout<<"s1 is /""<<s1<<"/"; s2 is /""<<s2
- <<"/"; s3 is empty /n"
- <<"The results of comparing s2 and s1:"
- <<"/ns2==s1 yields "<<(s2==s1)
- <<"/ns2!=s1 yields "<<(s2!=s1)
- <<"/ns2>s1 yields "<<(s2>s1)
- <<"/ns2<s1 yields "<<(s2<s1)
- <<"/ns2>=s1 yields "<<(s2>=s1)
- <<"/ns2<=s1 yields "<<(s2<=s1)<<"/n/n";
-
- cout<<"Testing! s3:/n";
- if(!s3)
- {
- cout<<"s3 is empty; assigning s1 to s3; /n";
- s3 = s1;
- cout<<"s3 is /""<<s3<<"/"/n/n";
- }
-
- cout<<"s1+=s2 yields s1=";
- s1+=s2;
- cout<<s1<<"/n/n";
-
- cout<<"s1+=/" to you/" yields/n";
- s1+=" to you";
- cout<<"s1="<<s1<<"/n/n";
-
- cout<<"The substring of s1 starting at /n"
- <<"location 0 for 14 characters, s1(0, 14), is:"
- <<s1(0, 14)<<"/n/n";
-
- cout<<"The substring of s1 starting at /n"
- <<"location 15, s1(15, 0) is:"
- <<s1(15, 0)<<"/n/n";
-
- String * s4Ptr = new String(s1);
- cout<<"*s4Ptr="<<*s4Ptr<<"/n/n";
-
- cout<<"assigning *s4Ptr to *s4Ptr/n";
- *s4Ptr = *s4Ptr;
- cout<<"*s4Ptr="<<*s4Ptr<<endl;
-
- delete s4Ptr;
-
- s1[0] = 'H';
- s1[6] = 'B';
-
- int a;
- cin>>a;
- return a;
-
- }
程序运行效果:
----------------------------------------------------------------------------------------------------------
一个有用的ANSI字符串类String的实现及使用演示程序
by 一剑(Loomman),QQ:28077188,MSN: [email protected] QQ裙:30515563
----------------------------------------------------------------------------------------------------------
Convention constructor: happy
Convention constructor: birthday
Convention constructor:
s1 is " happy"; s2 is " birthday"; s3 is empty
The results of comparing s2 and s1:
s2==s1 yields 0
s2!=s1 yields 1
s2>s1 yields 0
s2<s1 yields 1
s2>=s1 yields 0
s2<=s1 yields 1
Testing! s3:
s3 is empty; assigning s1 to s3;
operator = called
s3 is " happy"
s1+=s2 yields s1= happy birthday
s1+=" to you" yields
Convention constructor: to you
Destructor: to you
s1= happy birthday to you
Convention constructor:
The substring of s1 starting at
location 0 for 14 characters, s1(0, 14), is: happy birthda
Convention constructor:
The substring of s1 starting at
location 15, s1(15, 0) is: to you
Copy constructor: happy birthday to you
*s4Ptr= happy birthday to you
assigning *s4Ptr to *s4Ptr
operator = called
Attempt assignment of a String to itself
*s4Ptr= happy birthday to you
Destructor: happy birthday to you