文档详情

C++中String类的实现.docx

发布:2017-05-23约4.99千字共8页下载文档
文本预览下载声明
#include iostream#include string.husing namespace std;classMyString{char* m_str;intm_nLength;void _InitString();public://1. 默认构造函数MyString();// 2. 带参构造函数MyString( unsigned int length, const char ch );MyString(const char* str);MyString( const char* str, unsigned int length );MyString( const MyString str, unsigned int index, unsigned int length );// 3. 拷贝构造函数MyString( const MyString s );//4.析构函数~MyString();// 5. 追加字符串函数MyString append( const MyString _str );MyString append( const char* str );MyString append( const MyString str, unsigned int index, unsigned intlen );MyString append( const char* str, unsigned int num );MyString append( unsigned int num, char ch );/*// 6. 赋值函数MyString assign( const MyString str );MyString assign( const char* str );MyString assign( const char* str, unsigned int num );MyString assign( const MyString str, unsigned int index, unsigned intlen );// // 7. 返回字符串指针函数const char* c_str() const;// 8. 字符串比较函数intMyString::compare( const MyString str );int compare( const char* str );int compare( unsigned int index, unsigned int length, const MyString str );// 9. 拷贝函数unsignedint copy( char* str, unsigned int num, unsigned int index );// 10. 查找函数unsignedint find( const MyString str, unsigned int index );unsignedint find( const char* str, unsigned int index );unsignedint find( char ch, unsigned int index );// 11. 替换函数void replace(const char* findString, const char* replaceString);// 12. 返回子串MyStringsubstr( unsigned int index, unsigned int num );// // 13. 交换字符串函数void swap(MyString from );// 14. 其他函数void clear();unsignedint length() const;unsignedint size() const;// 15. 重载运算符// 1). 比较运算friendbool operator==(const MyString c1, const MyString c2);friendbool operator!=(const MyString c1, const MyString c2);// // 2). 追加运算friendMyString operator+(const MyString s1, const MyString s2 );friendMyString operator+(const char* s, const MyString s2 );// 3). 输入输出运算friendostream operator( ostream os, const MyString s );friendistream operator( istream is, MyString s );
显示全部
相似文档