今天考虑一个问题:
string *p = new string[5];
p[0] = “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”;
p[2] = “bb”;
new操作时是需要知道类型的大小的。但new string[5]时怎么知道string的大小的呢? 感觉很奇怪。
经过思考,认为原因如下:
1. new string[5]会调用string的默认构造函数。所以会有5个空字符串被创建。
2. string 的=操作会重新new char[], 更新原来的string内部的char*。
另外,string的实现参考:
#include <iostream>
#include <string.h>
using namespace std;
class MyString
{
char* m_str;
int m_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 int len );
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 int len );
//
// 7. 返回字符串指针函数
const char* c_str() const;
// 8. 字符串比较函数
int MyString::compare( const MyString& str );
int compare( const char* str );
int compare( unsigned int index, unsigned int length, const MyString& str );
// 9. 拷贝函数
unsigned int copy( char* str, unsigned int num, unsigned int index );
// 10. 查找函数
unsigned int find( const MyString& str, unsigned int index );
unsigned int find( const char* str, unsigned int index );
unsigned int find( char ch, unsigned int index );
// 11. 替换函数
void replace(const char* findString, const char* replaceString);
// 12. 返回子串
MyString substr( unsigned int index, unsigned int num );
// // 13. 交换字符串函数
void swap(MyString& from );
// 14. 其他函数
void clear();
unsigned int length() const;
unsigned int size() const;
// 15. 重载运算符
// 1). 比较运算
friend bool operator==(const MyString& c1, const MyString& c2);
friend bool operator!=(const MyString& c1, const MyString& c2);
//
// 2). 追加运算
friend MyString operator+(const MyString& s1, const MyString& s2 );
friend MyString operator+(const char* s, const MyString& s2 );
// 3). 输入输出运算
friend ostream& operator<<( ostream& os, const MyString& s );
friend istream& operator>>( istream& is, MyString& s );
//
// 4). 赋值运算
MyString& operator=( const MyString& s );
MyString& operator=( const char* s );
char& operator[]( unsigned int index );
*/
//显示结果
void Show()
{
cout<<m_str<<endl;
}
};
MyString::MyString()
{
m_str=new char[1];
m_str[0]=0;
int m_nLength=0;
}
MyString::~MyString()
{
delete []m_str;
m_str=NULL;
}
MyString::MyString( unsigned int length, const char& ch )
{
m_nLength=length;
m_str=new char[m_nLength+1];
for (int i=0;i<m_nLength;++i)
{
m_str[i]=ch;
}
m_str[m_nLength]=0;
}
MyString::MyString(const char* str)
{
if (NULL==str)
{
m_str=new char[1];
m_str[0]=0;
int m_nLength=0;
}
else
{
m_str=new char[strlen(str)+1];
strcpy(m_str,str);
m_nLength=strlen(str);
}
}
MyString::MyString( const char* str, unsigned int length )
{
if (NULL==str)
{
m_str=new char[1];
m_str[0]=0;
int m_nLength=0;
}
else
{
m_nLength=length;
m_str=new char[m_nLength+1];
strncpy(m_str,str,m_nLength);
m_str[m_nLength]=0;
}
}
MyString::MyString( const MyString& str, unsigned int index, unsigned int length )
{
m_nLength=length;
m_str=new char[m_nLength+1];
strncpy(m_str,str.m_str+index,m_nLength);
m_str[m_nLength]=0;
}
MyString::MyString( const MyString& s )
{
m_str= new char[strlen(s.m_str)+1];
strcpy(m_str,s.m_str);
}
/*
MyString::MyString& append( const MyString& _str )
{
}
*/
MyString& MyString::append( const char* str )
{
if (NULL==str)
{
m_str=new char[1];
m_str[0]=0;
}
else
{
strcat(m_str,str);
}
return *m_str;
}
MyString& MyString::append( const MyString& _str )
{
char*p=new char[strlen(m_str)+strlen(_str.m_str)+1];
strcpy(p,m_str);
strcat(p,_str.m_str);
p[strlen(m_str)+strlen(_str.m_str)]=0;
delete []m_str;
m_str=p;
return *this;
}
MyString& MyString::append( const char* str )
{
char*p=new char[strlen(m_str)+strlen(str)+1];
strcpy(p,m_str);
strcat(p,str);
p[strlen(m_str)+strlen(str)]=0;
delete []m_str;
m_str=p ;
return *this;
}
MyString& MyString::append( const MyString& str, unsigned int index, unsigned int len )
{
char*p=new char[strlen(m_str)+strlen(str.m_str)-index+1];
strcpy(p,m_str);
strncat(p,str.m_str+index,len);
p[strlen(m_str)+strlen(str.m_str)-index]=0;
delete []m_str;
m_str=p;
return *this;
}
MyString& MyString::append( const char* str, unsigned int num )
{
char*p=new char[num+1];
strncpy(p,str,num);
p[num]=0;
delete []m_str;
m_str=p;
return*this;
}
MyString& MyString::append( unsigned int num, char ch )
{
char*p=new char[num+1];
for(int i=0;i<num;++i)
{
p[i]=ch;
}
p[num]=0;
delete []m_str;
m_str=p;
return *this;
}
MyString& MyString::assign( const MyString& str )
{
<span style="color:#FF0000;"> char*p=new char[strlen(str.m_str)+1];
strcpy(p,str.m_str);
p[strlen(str.m_str)]=0;
delete []m_str;
m_str=p;
return *this;
}
MyString& MyString::assign( const char* str )
{
char*p=new char[strlen(str)+1];
strcpy(p,str);
p[strlen(str)]=0;
delete []m_str;
m_str=p;
return *this;
}
MyString& MyString::assign( const char* str, unsigned int num )
{
char*p=new char[num+1];
strncpy(p,str,num);
p[num]=0;
delete []m_str;
m_str=p;
return *this;
}
MyString& MyString::assign( const MyString& str, unsigned int index, unsigned int len )
{
char*p=new char[len+1];
strncpy(p,str.m_str+index,len);
p[len]=0;
delete []m_str;
m_str=p;
return *this;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/7468.html