string類的實現

2021-08-09 01:30:33 字數 2250 閱讀 6185

#include#include#includeusing namespace std;

class string

else

}/*~string()

}*/string(const string& s);//拷貝建構函式

string& operator=(const string& s);//賦值運算子=過載

//string& operator=(string& s);

//比較字串的大小

void strcmp(const string& s);

bool operator>(const string& s);

bool operator

bool operator==(const string& s);

bool operator!=(const string& s);

size_t sizeof()const;//字串所佔的位元組

size_t strlen()const;//字串長度

char& operator(size_t index);//下標的過載

void strcpy(const string& s);//字串的拷貝

string& operator+=(const string& s);//字串的連線

bool strstr(const string& s);//尋找子串

void strstr(const string& s);

void display() }

private:

char* m_pstr;

};//拷貝建構函式

string::string(const string& s)

:m_pstr(new char[strlen(s.m_pstr) + 1])

//string::string(const string& s)

//: m_pstr()

////賦值運算子=過載

string& string::operator=(const string& s)

else

return *this;

}//string& string::operator=(string& s)

////字串比較大小

void string::strcmp(const string& s)

else

return false;

}bool string::operator

else

return false;

}bool string::operator==(const string& s)

else

return false;

}bool string::operator!=(const string& s)

else

return true;

}size_t string::sizeof()const//包含'\0'

return count;

}size_t string::strlen()const//不包含'\0'

return count;

}char& string::operator(size_t index)

void string::strcpy(const string& s)

else if (s.m_pstr == null)

else }

}string& string::operator+=(const string& s)//strcat

else if (s.m_pstr == null)

else

while (*s2)

*p = '\0';

} *this = str.m_pstr;

return *this;

}bool string::strstr(const string& s)//尋找子串

else

if (*s2 == '\0')

if (*s1 == '\0')

s3++;

} }return false;

}void string::strstr(const string& s)

else }

void test()

int main()

運算結果如下:

string類的實現

參考c primer.string類的實現,清翔兔 06,jan.includeusing namespace std class string string void private char m data inline string string const char str inline st...

String類的實現

學習資料結構寫了乙個string的類,貼出來求指教 ifndef string h h define string h h include include include define defaultsize 128 class string maxsize為傳入引數的string string c...

string類的實現

include using namespace std class string public string const char str 0 普通建構函式 string const string other 拷貝建構函式 string void 析構函式 string operator const...