C++ string
string 的C++代码实现
class string{
//构造函数
string(char* data) {
if(m_data == null || strlen(data) == 0) {
m_data = new char[1];
*m_data = ‘\0’;
} else {
int len = strlen(data);
m_data = new char[len+1];
strcpy(m_data,data);
}
}
//拷贝构造函数
string(const string& others){
int m = strlen(others.m_data);
m_data = new char[m+1];
strcpy(m_data, others.m_data);
}
//析构函数
~string(){
delete[] m_data;
}
//拷贝复制函数
string& operator= (const string& others){
if (this == &other) {
return *this;
}
delete[] m_data;
int len = strlen(others.m_data);
m_data = new char[len+1];
strcpy(m_data, others.m_data);
return *this;
}
char* m_data;
}
vector增长的源码实现