題目要求實作Str類的成員函式,Str類定義如下:
class Str{
public:
Str() ; //默認建構式
Str(const char* s) ; //由字串常量構造的建構式 , 如 Str s("aaa") ;
Str(const Str& s) ; //拷貝建構式
~Str() ; //解構式
int len()const; //回傳字串長度,即包含了幾個有效字符,不包括末尾'\0' .
bool empty()const ; //回傳是否為空
void append( char ch ) ; //向字串末尾添加字符 , 如 s = "aaa"時 ,s.append('b') 后 , s = "aaab" ;
void clear() ; // 清空該字串
Str substr( int position , int count ) const ; //回傳從pos位置開始,連續count個字符的子串。即 data[pos , pos+count) ,左閉右開區間。
Str operator+(const Str& s)const ; //多載+號運算子, 使得 "aa" + "b" + "cd" = "aabcd" ;
bool operator==(const Str& s)const ; //多載==運算子,判斷兩個字串是否相等。 如果s1.data="https://bbs.csdn.net/topics/aba", s2.data="https://bbs.csdn.net/topics/aba" , 那么s1==s2 ;
Str operator=(const Str &s) ; //多載'='賦值運算子,把字串s的內容賦值給自己.
char operator[](int index) const ; //多載'[]'運算子,回傳 data[index] ;
void print()const{
for( int i = 0 ; i < length ; i++ )
cout << data[i] ;
cout << endl ;
}
private:
char* data ;
int length ;
};
主函式如下:
int main(){
int N ;
cin >> N ;
Str s ;
while(N--){
char c_str[100] ;
scanf("%s" , c_str ) ;
int pos , count ;
cin >> pos >> count ;
Str tmp( c_str ); //由字符陣列構造一個物件 , 呼叫 Str:: Str(const char* s) ;
tmp.print() ; //列印
Str tmp2 = tmp.substr( pos, count ) ; //取一段子串
tmp2.print() ; //列印
Str tmp3 ;
for( int i = 0 ; i < count && i < tmp.len() - pos ; i++ )
tmp3.append( tmp[i+pos] ) ; //在字串后增添char
tmp3.print() ; //列印
cout << ( tmp2 == tmp3 ) << endl ; //判斷是否相等
Str tmp4(tmp) ; //拷貝建構式
tmp = s + tmp ; //字串拼接
s.clear() ; //清空s
for(int i = 0 ; i < tmp.len() ; i++)
s.append(tmp[i]) ; //把tmp添加到s
s.print() ; //列印
cout << ( s == tmp4 ) << endl ; //判斷是否相等
tmp.clear() ; //清空tmp
cout << tmp.empty() << endl ; //判斷是否為空
cout << endl ;
}
}
我對成員函式的實作如下:
Str::Str(){
data = new char[10000];
length = 0;
}
Str::Str(const char* s){
length = strlen(s);
if (length > 10000){
data = new char[length * 2];
}
else
data = new char[10000];
memcpy(data, s, length*sizeof(char));
data[length] = '\0';
}
Str::Str(const Str& s){
length = s.length;
if (length>10000){
data = new char[length * 2];
}
else
data = new char[10000];
memcpy(data, s.data, length*sizeof(char));
data[length] = '\0';
}
Str::~Str(){
if (data)
delete[]data;
}
int Str::len()const{
return length;
}
bool Str::empty()const{
if (length == 0) return true;
else return false;
}
void Str::append(char ch){
if (length + 1 > 10000){
char *tem = new char[length * 2];
memcpy(tem, data, length*sizeof(char));
}
data[length] = ch;
length++;
data[length] = '\0';
}
void Str::clear(){
if (data)
delete[]data;
data = new char[10000];
length = 0;
}
Str Str::substr(int position, int count) const {
if (position + count > length) count = length - position;
char *tem;
if (count > 10000) {
tem = new char[count * 2];
}
else{
tem = new char[10000];
}
memcpy(tem, data + position, count*sizeof(char));
tem[count] = '\0';
Str temsrt(tem);
return temsrt;
}
Str Str::operator+(const Str& s) const{
int total = s.length + length;
char *tem;
if (total > 10000)
tem = new char[total * 2];
else tem = new char[10000];
memcpy(tem, data, length*sizeof(char));
memcpy(tem+length, s.data, s.length*sizeof(char));
tem[total] = '\0';
Str temstr(tem);
return temstr;
}
Str Str::operator=(const Str& s){
if (s.length>10000){
if (data)
delete[]data;
data = new char[s.length * 2];
}
length = s.length;
memcpy(data, s.data, length*sizeof(char));
data[length] = '\0';
return *this;
}
bool Str::operator==(const Str& s)const{
if (length != s.length) return false;
else{
if (data!=NULL&&s.data!=NULL)
for (int i = 0; i < length; i++){
if (data[i] != s.data[i]) return false;
}
return true;
}
}
char Str::operator[](int position)const{
return data[position];
}
data的基本長度定義為10000是因為測驗案例非常惡心,參與運算的總字符居然達到了10w(據說)。
如果修改一次data就要重新分配一次空間就會出現超時現象。
我的代碼的主要問題是出現在clear函式:我測驗了一下,當data的長度達到10051的時候會在clear處的delete處斷開:


請問各位應該如何改動這段程式??(如果把delete那段程式刪掉會造成記憶體泄漏導致的程式崩潰)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/218860.html
標籤:C++ 語言
