一个有用的ANSI字符串类String的实现及使用演示

来源:互联网 发布:雅思备考资料 知乎 编辑:程序博客网 时间:2024/06/05 22:41

和C#不一样,C和C++的内部都没有字符串数据类型,但是我们可以用C++建立一个实现字符串相关操作的类型:String

并且利用重载的机制提供方便操作字符串的相关运算符集。

 

下面的程序分成两个部分:

(1)String类:类头String.h和类实现String.cpp

(2)String类使用演示程序Main.cpp

 

类头文件String.h代码如下:

  1. #ifndef STRING_H
  2. #define STRING_H
  3. #include <iostream>
  4. using namespace std;
  5. class String
  6. {
  7.     friend ostream & operator<<(ostream & output, const String & s);
  8.     friend istream & operator>>(istream & input, String & s);
  9. public:
  10.     String(const char* = "");
  11.     String(const String &);
  12.     ~String();
  13.     const String & operator=(const String &);   //赋值
  14.     String & operator+=(const String &);        //字符串连接
  15.     int operator!() const;                      //String为空?
  16.     int operator==(const String &) const;       //测试s1==s2
  17.     int operator!=(const String &) const;       //测试s1!=s2
  18.     int operator<(const String &) const;        //测试s1<s2
  19.     int operator>(const String &) const;        //测试s1>s2
  20.     int operator<=(const String &) const;       //测试s1<=s2
  21.     int operator>=(const String &) const;       //测试s1>=s2
  22.     char & operator[](int);                     //返回对字符的引用
  23.     String & operator()(intint);              //返回一个子字符串
  24.     int GetLength() const;                      //返回字符串的长度,不包括结尾的/0
  25. private:
  26.     char* sPtr;                                 //指向字符串起始位置的指针
  27.     int length;                                 //字符串的长度
  28. };
  29. #endif

类实现文件String.cpp代码如下:

  1. #include <iostream>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "String.h"
  5. ostream & operator<<(ostream & output, const String & s)
  6. {
  7.     output<<s.sPtr;
  8.     return output;
  9. }
  10. istream & operator>>(istream & input, String & s)
  11. {
  12.     static char temp[100];
  13.     input>>temp;
  14.     s = temp;
  15.     return input;
  16. }
  17. String::String(const char * s)
  18. {
  19.     cout<<"Convention constructor:"<<s<<endl;
  20.     length = strlen(s);
  21.     sPtr = new char[length+1];
  22.     assert(sPtr!=0);
  23.     strcpy(sPtr, s);
  24. }
  25. String::String(const String & copy)
  26. {
  27.     cout<<"Copy constructor:"<<copy.sPtr<<endl;
  28.     length = copy.length;
  29.     sPtr = new char[length+1];
  30.     assert(sPtr != 0);
  31.     strcpy(sPtr, copy.sPtr);
  32. }
  33. String::~String()
  34. {
  35.     cout<<"Destructor:"<<sPtr<<endl;
  36.     delete[] sPtr;
  37. }
  38. const String & String::operator=(const String & right)
  39. {
  40.     cout<<"operator = called"<<endl;
  41.     if(&right != this)   //避免自我赋值
  42.     {
  43.         delete[] sPtr;
  44.         length = right.length;
  45.         sPtr = new char[length+1];
  46.         assert(sPtr!=0);
  47.         strcpy(sPtr, right.sPtr);
  48.     }
  49.     else
  50.     {
  51.         cout<<"Attempt assignment of a String to itself/n";
  52.     }
  53.     return *this;
  54. }
  55. String & String::operator+=(const String & right)
  56. {
  57.     char* tempPtr = sPtr;
  58.     length += right.length;
  59.     sPtr = new char[length+1];
  60.     assert(sPtr!=0);
  61.     strcpy(sPtr, tempPtr);
  62.     strcat(sPtr, right.sPtr);
  63.     delete[] tempPtr;
  64.     return *this;
  65. }
  66. //字符串为空?
  67. int String::operator !() const
  68. {
  69.     return length == 0;
  70. }
  71. int String::operator==(const String & right) const
  72. {
  73.     return strcmp(sPtr, right.sPtr)==0;
  74. }
  75. int String::operator!=(const String & right) const
  76. {
  77.     return strcmp(sPtr, right.sPtr)!=0;
  78. }
  79. int String::operator<(const String & right) const
  80. {
  81.     return strcmp(sPtr, right.sPtr)<0;
  82. }
  83. int String::operator>(const String & right) const
  84. {
  85.     return strcmp(sPtr, right.sPtr)>0;
  86. }
  87. int String::operator<=(const String & right) const
  88. {
  89.     return strcmp(sPtr, right.sPtr)<=0;
  90. }
  91. int String::operator>=(const String & right) const
  92. {
  93.     return strcmp(sPtr, right.sPtr)>=0;
  94. }
  95. //返回对String中某个字符的引用
  96. char & String::operator [](int subscript)
  97. {
  98.     //首先测试下标是否越界
  99.     assert(subscript>=0 && subscript<length);
  100.     return sPtr[subscript];
  101. }
  102. String & String::operator()(int index, int sublength)
  103. {
  104.     //首先测试下标是否越界
  105.     assert(index>=0 && index<length && sublength >= 0);
  106.     String * subPtr = new String;
  107.     assert(subPtr!=0);
  108.     //计算子字符串长度
  109.     int size = 0;
  110.     if(sublength == 0 || (index+sublength>length))
  111.     {
  112.         size = length - index + 1;
  113.     }
  114.     else
  115.     {
  116.         size = sublength + 1;
  117.     }
  118.     //分配子字符串内存
  119.     delete subPtr->sPtr;
  120.     subPtr->length = size;
  121.     subPtr->sPtr = new char[size];
  122.     assert(subPtr->sPtr!=0);
  123.     int i = index, j=0;
  124.     //把子字符串拷贝到新的对象中
  125.     for( ; i<index+size-1; i++, j++)
  126.     {
  127.         subPtr->sPtr[j] = sPtr[i];
  128.     }
  129.     subPtr->sPtr[j] = '/0';
  130.     return *subPtr;
  131. }
  132. int String::GetLength() const
  133. {
  134.     return length;
  135. }

String类使用演示程序Main.cpp代码如下:

  1. #include <iostream>
  2. #include "String.h"
  3. int main()
  4. {
  5.     cout<<"---------------------------------------------------------------------------------------------------------/n"
  6.     cout<<"                   一个有用的ANSI字符串类String的实现及使用演示程序                    /n"
  7.     cout<<"by 一剑(Loomman),QQ:28077188,MSN: Loomman@hotmail.com QQ裙:30515563/n"
  8.     cout<<"---------------------------------------------------------------------------------------------------------/n/n"
  9.     String s1(" happy"), s2(" birthday"), s3;
  10.     cout<<"s1 is /""<<s1<<"/"; s2 is /""<<s2
  11.         <<"/"; s3 is empty /n"
  12.         <<"The results of comparing s2 and s1:"
  13.         <<"/ns2==s1 yields "<<(s2==s1)
  14.         <<"/ns2!=s1 yields "<<(s2!=s1)
  15.         <<"/ns2>s1 yields "<<(s2>s1)
  16.         <<"/ns2<s1 yields "<<(s2<s1)
  17.         <<"/ns2>=s1 yields "<<(s2>=s1)
  18.         <<"/ns2<=s1 yields "<<(s2<=s1)<<"/n/n";
  19.     cout<<"Testing! s3:/n";
  20.     if(!s3)
  21.     {
  22.         cout<<"s3 is empty; assigning s1 to s3; /n";
  23.         s3 = s1;
  24.         cout<<"s3 is /""<<s3<<"/"/n/n";
  25.     }
  26.     cout<<"s1+=s2 yields s1=";
  27.     s1+=s2;
  28.     cout<<s1<<"/n/n";
  29.     cout<<"s1+=/" to you/" yields/n";
  30.     s1+=" to you";
  31.     cout<<"s1="<<s1<<"/n/n";
  32.     cout<<"The substring of s1 starting at /n"
  33.         <<"location 0 for 14 characters, s1(0, 14), is:"
  34.         <<s1(0, 14)<<"/n/n";
  35.     cout<<"The substring of s1 starting at /n"
  36.         <<"location 15, s1(15, 0) is:"
  37.         <<s1(15, 0)<<"/n/n";
  38.     String * s4Ptr = new String(s1);
  39.     cout<<"*s4Ptr="<<*s4Ptr<<"/n/n";
  40.     cout<<"assigning *s4Ptr to *s4Ptr/n";
  41.     *s4Ptr = *s4Ptr;
  42.     cout<<"*s4Ptr="<<*s4Ptr<<endl;
  43.     delete s4Ptr;
  44.     s1[0] = 'H';
  45.     s1[6] = 'B';
  46.     int a;
  47.     cin>>a;
  48.     return a;
  49. }

程序运行效果:

----------------------------------------------------------------------------------------------------------
      一个有用的ANSI字符串类String的实现及使用演示程序
      by 一剑(Loomman),QQ:28077188,MSN: Loomman@hotmail.com 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