自己实现的一个简单的string类,原来以为挺简单的,真正写起来才发现,还是有很多细枝末节的东西没有考虑清楚哦;
比如:
1.insert的在pos处插入是表示 "插入在索引为pos的元素之前"
比如对于s1="abcd", s1.insert(2, f), s1变成了"abfcd"
2.remove(char c)这个接口表示删除simple_string中所有的c.
比如s1="aaabb", s1.remove('a'), s1值为"bb"
3.remove(int p1, int p2), 表示删除[p1, p2)这样的前闭后开区间,
比如s1="aaabb", s1.remove(2, 3), s1值为"aabb"
还有最重要一点:我原来想的是不申请new空间,直接在原来的字符串上操作,比如 operator +=操作,直接在str的后面增加字符,这样确实能得到结果,但是测试用例一运行结束就崩溃,后来才发现,是在析构的时候出错了,调用delete会崩溃。应该是delete只能销毁最开始str new申请的空间,后来增加的是不能被销毁的,所以出错了。所以只能老老实实的new一块临时内存,然后把str了新增的部分都copy进去,再把tmp赋值给str了。
这是oj.h
#ifndef __OJ_H__
#define __OJ_H__
#include<iostream>
//#include<cstring>
//#include<cmath>
//#include<iomanip>
#include<stdlib.h>
//using namespace std;
class simple_string
{
private:
char *str;
int len;
public: //构造函数
~simple_string();
simple_string();
simple_string(simple_string &);
simple_string(char *);
simple_string(int n);
public: //运算符重载
bool operator == (simple_string &);
bool operator != (simple_string &);
//下面的函数仅仅给出函数声明,请实现其功能
public: //功能函数
void insert(int ,char); //指定位置插入字符
void insert(int pos,char *ch); //指定位置插入字符串
void insert(int pos,simple_string& ch); //指定位置插入string
void remove(char);
void remove(int p1,int p2,char c);
void remove(int fig);
void remove(int p1,int p2);
public: //运算符重载
simple_string &operator =(simple_string &);
simple_string &operator =(char *);
simple_string &operator =(char );
simple_string operator + (char *ch);
simple_string operator + (char );
void operator += (simple_string &);
void operator += (char *);
void operator += (char );
};
#endif
这是oj.cpp
#include "oj.h"
///
// 构造函数
//
simple_string::simple_string()
{
str=new char[1];
len=0;
str[0]=NULL;
}
simple_string::~simple_string()
{
delete []str;
}
simple_string::simple_string(char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
str[len]=NULL;
}
simple_string::simple_string(simple_string &i_str)
{
len=i_str.len;
str=new char[len+1];
strcpy(str,i_str.str);
str[len]=NULL;
}
simple_string::simple_string(int n)
{
len=n;
str=new char[len+1];
for(int i=0;i<n;i++) str[i]='0';
str[len]=NULL;
}
bool simple_string::operator == (simple_string &ch)
{
if(strcmp(str,ch.str)==0) return true;
return false;
}
bool simple_string::operator != (simple_string &ch)
{
if(strcmp(str,ch.str)!=0) return true;
return false;
}
/
//请实现下面方法
//
//ok
simple_string& simple_string::operator =(simple_string &ch)
{
if(this == &ch)
{
return *this;
}
len = ch.len;
delete(str);
str = new char[len + 1];
strcpy(str, ch.str);
str[len] = NULL;
return *this;
}
//ok
simple_string& simple_string::operator =(char *ch)
{
len = strlen(ch);
delete(str);
str = new char[len+1];
strcpy(str, ch);
str[len] = NULL;
return *this;
}
//ok
simple_string& simple_string::operator =(char c)
{
len = 1;
delete(str);
str = new char[2];
*str = c;
str[1] = NULL;
return *this;
}
//ok
void simple_string::operator +=(simple_string &ch)
{
char *temp = new char[len + ch.len +1];
strcpy(temp,str);
strcat(temp, ch.str);
delete []str;
str = temp;
len += ch.len;
str[len] = NULL;
}
//ok
void simple_string::operator += (char *ch)
{
int len_ch = strlen(ch);
char *temp = new char[len + len_ch +1];
strcpy(temp,str);
strcat(temp, ch);
delete []str;
str = temp;
len += len_ch;
str[len] = NULL;
}
//ok
//返回的是类类型,而不是引用,所以执行S0 = S0 + "DEFG"时,S0+"DEFG"返回的是S0的拷贝,而不是S0本身
simple_string simple_string::operator + (char *ch)
{
int len_ch = strlen(ch);
char *temp = new char[len + len_ch +1];
strcpy(temp,str);
strcat(temp, ch);
delete []str;
str = temp;
len += len_ch;
str[len] = NULL;
return *this;
}
//ok
void simple_string::operator += (char ch)
{
if(ch == NULL)
return;
len++;
char *temp = new char[len + 1];
strcpy(temp,str);
temp[len-1] = ch;
temp[len] = NULL;
delete []str;
str = temp;
}
//ok
simple_string simple_string::operator + (char ch)
{
if(ch == NULL)
return *this;
len++;
char *temp = new char[len + 1];
strcpy(temp,str);
temp[len-1] = ch;
temp[len] = NULL;
delete []str;
str = temp;
return *this;
}
//ok
//指定位置之前插入字符
void simple_string::insert(int pos,char c)
{
len++;
char* temp = new char[len+1];
int i = 0;
for(i = 0; i < pos; i++ )
{
temp[i] = str[i];
}
temp[pos] = c;
for(i = pos+1; i < len; i++)
{
temp[i] = str[i-1];
}
temp[len] = NULL;
delete []str;
str = temp;
}
//指定位置插入字符串
//ok
void simple_string::insert(int pos,char *ch)
{
int len_ch = strlen(ch);
len += len_ch;
char* temp = new char[len+1];
int i = 0;
for(i = 0; i < pos; i++ )
{
temp[i] = str[i];
}
for(i = 0; i < len_ch; i++)
{
temp[i+pos] = ch[i];
}
for(i = pos+len_ch; i < len; i++)
{
temp[i] = str[i-len_ch];
}
temp[len] = NULL;
delete []str;
str = temp; ;
}
//指定位置插入string
//ok
void simple_string::insert(int pos,simple_string& ch)
{
int len_ch = ch.len;
len += len_ch;
char* temp = new char[len+1];
int i = 0;
for(i = 0; i < pos; i++ )
{
temp[i] = str[i];
}
for(i = 0; i < len_ch; i++)
{
temp[i+pos] = ch.str[i];
}
for(i = pos+len_ch; i < len; i++)
{
temp[i] = str[i-len_ch];
}
temp[len] = NULL;
delete []str;
str = temp; ;
}
//ok
//删除指定字符c
void simple_string::remove(char c)
{
int i, j;
char *tmp = new char[len + 1];
for(i = 0, j =0; i < len; i++)
{
if(str[i] != c)
{
tmp[j++] = str[i];
}
}
tmp[j] = NULL;
len = strlen(tmp);
delete []str;
str = tmp;
}
//删除指定位置p1,p2之间的字符c。
void simple_string::remove(int p1,int p2,char c)
{
int i,j;
char *tmp = new char[len + 1];
for(i= 0,j=0; i < p1; i++)
{
tmp[j++] = str[i];
}
for(i = p1; i< p2; i++)
{
if(str[i] != c)
{
tmp[j++] = str[i];
}
}
for(i = p2; i< len; i++)
{
tmp[j++] = str[i];
}
tmp[j] = NULL;
len = strlen(tmp);
delete []str;
str = tmp;
}
//删除第p1位置上的字符。
void simple_string::remove(int p1)
{
int i;
for(i = p1; i < len-1; i++)
{
str[i] = str[i+1];
}
str[len-1] = NULL;
len--;
}
删除指定位置p1,p2之间的所有字符,
void simple_string::remove(int p1,int p2)
{
int len_tmp = len -(p2 - p1);
char *tmp = new char[len_tmp + 1];
int i,j;
for(i = 0; i <p1; i++)
{
tmp[i] = str[i];
}
for(i = p1, j = p2;i<len_tmp, j < len; i++,j++)
{
tmp[i] = str[j];
}
tmp[len_tmp] = NULL;
delete []str;
str = tmp;
len = len_tmp;
}