要求:
模拟实现c++库函数里面string的部分功能,实现增删查改,比较预算符的重载
#ifndef __MySTRING_H__
#define __MYSTRING_H__
#define _CRT_SECURE_NO_DEPRECATE 1
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
using namespace std;
class String
{
public:
String(const char* str)
:_str(new char[strlen(str)+1])
,_size(strlen(str)+1)
,_capacity(_size)
{
strcpy(_str,str);
}
String(const String& d)
:_str(new char[strlen(d._str)+1])
,_size(d._size)
,_capacity(d._capacity)
{
strcpy(_str,d._str);
}
~String()
{
delete[] _str;
_str = NULL;
_size = 0;
_capacity = 0;
}
//s1 = s2
//1构造一个和目标对象一样的对象
//然后交换两个对象
void Swap(String& d)
{
char* tmpchar = this->_str;
this->_str = d._str;
d._str = tmpchar;
size_t tmpsize = this->_size;
this->_size = d._size;
d._size = tmpsize;
size_t tmpcapacity = this->_capacity;
this->_capacity = d._capacity;
d._capacity = tmpcapacity;
}
String& operator=(const String& d)
{
if(_str == d._str)
{
return *this;
}
String tmp(d._str);
Swap(tmp);
return *this;
}
char* GetStr()
{
return _str;
}
void Expand(size_t inc);
void PushBack(char ch);
void PushBack(char* str);
void PopBack();
void Insert(size_t pos, char ch) ;
void Insert(size_t pos, const char* str);
void Erase(size_t pos, size_t count);
int Find(char ch)const;
int Find(const char* str) const;
bool operator==(const String& s) const;
bool operator!=(const String& s)const;
bool operator>(const String& s) const;
bool operator>=(const String& s) const;
bool operator<(const String& s) const;
bool operator<=(const String& s) const;
char& operator[](size_t pos);
private:
char* _str;
size_t _size;
size_t _capacity;
};
void String:: Expand( size_t inc)
{
_str = (char*)realloc(_str,_size+inc);
if(_str == NULL)
{
perror(_str);
}
_capacity += inc;
//cout<<"haha"<<endl;
}
void String:: PushBack(char ch )
{
if(_size == _capacity)
{
Expand(1);
}
_str[strlen(_str)] = ch;
_str[_size] = '\0';
++_size;
}
void String:: PushBack(char* str )
{
Expand(strlen(str));
strcpy((_str+_size-1),str);
_size+= strlen(str);
}
void String:: PopBack()
{
assert(strlen(_str)>=1);
_str[strlen(_str)-1] = '\0';
--_size;
}
void String:: Insert(size_t pos, char ch)
{
if(_size == _capacity)
{
Expand(4);
}
int i = 0;
while(_size-i > pos)
{
_str[_size-i] = _str[_size-i-1];
i++;
}
_str[pos] = ch;
++_size;
}
//0位置有问题
void String:: Insert(size_t pos, const char* str)
{
int len = strlen(str);
int end = strlen(_str);
int i = 0;
Expand(strlen(str));
while(end >= (int)pos) //把pos以后的数据统一向后移动len个单位
{
_str[end+len] = _str[end];
--end;
}
while (*str) //把数据放进去
{
_str[pos++] = *str++;
}
_size += len;
}
//1没有数据或者超出范围
//2在正常的范围内
//依次向前覆盖一个单位数据
void String:: Erase(size_t pos, size_t count) //删除pos位置开始的count个数据
{
assert(pos<=_size-1);
while(_str[pos+count-1])
{
_str[pos-1] = _str[pos+count-1];
pos++;
}
_str[pos-1] = '\0';
_size -=count;
}
//返回下标 从0开始
//1不存在字符串
//2字符串中不存在字符
//3字符传中存在字符
int String:: Find(char ch) const
{
assert(strlen(_str)!=0);
int count = 0;
char* tmpptr = _str;
while(*tmpptr != '\0' )
{
if(*tmpptr != ch)
{
count++;
}
else
{
return count;
}
tmpptr++;
}
return -1; //没找打
}
//返回下标从0开始
int String:: Find(const char* str) const //寻找字符串子串
{
assert(strlen(_str)>0 && str!=NULL && strlen(str)>0 );
char* tmp_str = _str;
char* tmpstr = (char*)str;
char* tmp;
int count = 0;
while(*tmp_str)
{
if(*tmp_str != *tmpstr)
{
*tmp_str++;
count++;
}
else
{
tmp = tmp_str; // 保存父串的位置
while(*tmp_str == *tmpstr && *tmp_str!= '\0' )
{
tmpstr++;
tmp_str++;
}
if(*tmpstr == '\0')
{
return count;
}
tmp_str = tmp+1;
}
}
return -1;
}
bool String:: operator==(const String& s) const
{
char* tmp_str = _str;
char* tmpstr = s._str;
while(*tmp_str == *tmpstr && *tmpstr != '\0' && *tmp_str != '\0')
{
tmp_str++;
tmpstr++;
}
if( *tmpstr == '\0' && *tmp_str == '\0')
{
return true;
}
return false;
}
bool String:: operator!=(const String& s)const
{
return !operator==(s);
}
bool String:: operator>(const String& s) const
{
assert(strlen(_str)>=1 && strlen(s._str)>=1);
char* tmp_str = _str;
char* tmpstr = s._str;
while(*tmp_str && *tmpstr)
{
if(*tmp_str>*tmpstr)
{
return true;
}
else if(*tmp_str<*tmpstr)
{
return false;
}
else
{
tmp_str++;
tmpstr++;
}
}
if(*tmp_str != '\0' )
{
return true;
}
return false;
}
bool String:: operator<(const String& s) const
{
assert(strlen(_str)>=1 && strlen(s._str)>=1);
return !(operator>(s) || operator==(s));
}
bool String:: operator<=(const String& s) const
{
assert(strlen(s._str)>=1 && strlen(_str)>=1);
return !(operator>(s));
}
bool String:: operator>=(const String& s) const
{
assert(strlen(_str)>=1 && strlen(s._str)>=1);
return !(operator<(s));
}
char& String:: operator[](size_t pos)
{
assert(pos<=strlen(_str));
return *(_str+pos);
}
#endif//__MySTRING_H__
测试用例没有写,有兴趣测试的可以自己写几个实验一下。功能我都是验证过的,可能有的边界条件没考虑到,发现问题的朋友可以留言,互相进步!