打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
string类实现(C++)


 1 class CMyString 2 { 3         friend std::ostream& operator<<( std::ostream& os, const CMyString& str); 4         private: 5                 char* m_pData; //  私有变量保存字符串 6         public: 7                 CMyString( const char* str = NULL ); // 构造函数 8                 CMyString( const CMyString& str ); // 拷贝构造函数 9                 ~CMyString( void ); // 析构函数10                 CMyString& operator=( const CMyString& str ); // 赋值运算符11                 CMyString operator+( const CMyString& str ); // 字符串连接 12                 bool operator==( const CMyString& str ); // 判断相等13                 char operator[]( int idx ); // 数组索引14                 int getLength(); // 返回长度15 };
  1 CMyString::CMyString( const char* str )  2 {  3         if ( !str )  4         {  5                 this->m_pData = 0;  6         }  7         else  8         {  9                 this->m_pData = new char[ strlen( str ) + 1 ]; 10                 strcpy( this->m_pData, str ); 11         } 12 } 13  14 CMyString::CMyString( const CMyString& str ) 15 { 16         if ( !str.m_pData ) 17         { 18                 this->m_pData = 0; 19         } 20         else 21         { 22                 this->m_pData = new char[ strlen( str.m_pData ) + 1 ]; 23                 strcpy( this->m_pData, str.m_pData ); 24         } 25 } 26  27 CMyString::~CMyString( void ) 28 { 29         if ( this->m_pData) 30         { 31                 delete[] this->m_pData; 32                 this->m_pData = 0; 33         } 34 } 35  36 CMyString& CMyString::operator=( const CMyString& str) 37 { 38         if ( this != &str ) 39         { 40                 delete[] this->m_pData; 41                 if ( !str.m_pData ) 42                 { 43                         this->m_pData = 0; 44                 } 45                 else 46                 { 47                         this->m_pData = new char[ strlen( str.m_pData ) + 1 ]; 48                         strcpy( this->m_pData, str.m_pData ); 49                 } 50         } 51         return *this; 52 } 53  54 CMyString CMyString::operator+( const CMyString& str ) 55 { 56         CMyString newString; 57         if ( !str.m_pData ) 58         { 59                 newString = *this; 60         } 61         else if ( !this->m_pData ) 62         { 63                 newString = str; 64         } 65         else 66         { 67                 newString.m_pData = new char[ strlen( this->m_pData ) + strlen( str.m_pData ) + 1 ]; 68                 strcpy( newString.m_pData, this->m_pData ); 69                 strcat( newString.m_pData, str.m_pData ); 70         } 71  72         return newString; 73  74 } 75  76 bool CMyString::operator==( const CMyString& str ) 77 { 78         if ( strlen(this->m_pData) != strlen( str.m_pData ) ) 79         { 80                 return false; 81         } 82         else 83         { 84                 return strcmp( this->m_pData, str.m_pData ) ? false : true; 85         } 86 } 87  88 char CMyString::operator[]( int idx) 89 { 90         if ( idx > 0 && idx < strlen( this->m_pData ) ) 91         return this->m_pData[idx]; 92 } 93  94 int CMyString::getLength() 95 { 96         return strlen(this->m_pData); 97 } 98  99 std::ostream& operator<<( std::ostream& os, const CMyString& str )100 {101         os<< str.m_pData;102         return os;103 }

 
 








本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C++提高:String 类实现
C++11 lambda表达式
C++ Primer 第三章补充String类的实现
c++函数返回引用
数据结构五:字符串String(C++代码)
strstr实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服