模拟实现string的增删查改

来源:互联网 发布:中国网络作家协会主席 编辑:程序博客网 时间:2024/03/29 19:47
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <cassert>


using namespace std;


class String
{
public:
String(char* str="")
:_str(new char[strlen(str)+1])
,_sz(strlen(str))
,_capacity(_sz)
{
strcpy(_str,str);
}


String(const String& s)
:_str(NULL)
,_sz(0)
,_capacity(0)

String tmp(s._str);
Swap(tmp);
}


String& operator=(String s)
{
Swap(s);
return *this;
}


~String()
{
cout<<"delete"<<endl;
delete[] _str;
}
char* GetStr()
{
return _str;
}


public:
void Swap(String& s)
{
swap(_str,s._str);
_sz = s._sz;
_capacity = s._capacity;
}
size_t Size()
{
return _sz;
}
size_t Capacity() 
{
return _capacity;
}


// 增删查改 
void PushBack(char ch)
{
if(_sz == _capacity)
{
Expand(_sz*2);
}
_str[_sz] = ch;
++_sz;
_str[_sz]='\0';
}
void PushBack(const char* str) 
{
size_t len= strlen(str);
if((_sz+len)> _capacity)
{
Expand(_sz+len);
}
strcpy((_str+_sz),str);
_sz += len;
}
void PopBack(char ch) 
{
int i = _sz;
if(_sz == _capacity)
{
Expand(_sz*2);
}
for(i = _sz;i >= 0;i --)
{
_str[i+1]=_str[i];
}
_str[0]=ch;
}


void Insert(size_t pos, char ch) 
{
if(_sz == _capacity)
{
Expand(_sz*2);
}
int end = _sz;
while(end >= (int)pos)
{
_str[end+1]=_str[end];
--end;
}
_str[pos]=ch;
++_sz;
}
void Insert(size_t pos, const char* str)
{
size_t len= strlen(str);
if((_sz+len)> _capacity)
{
Expand(_sz+len);
}
int end = _sz;
while(end >=(int)pos)
{
_str[end+len]=_str[end];
--end;
}
while(*str)
{
_str[pos++] = *(str++);
}
_sz+=len;
}
void Erase(size_t pos, size_t count)
{
if(pos+count >_sz)
{
_str[pos]='\0';
_sz=pos;
}
else
{
strcpy(_str+pos,_str+pos+count);
_sz-=count;
}
}
size_t Find(char ch) const 
{
size_t pos = 0;
while((_str[pos] != ch)&&(_str[pos] != '\0'))
{
pos+=1;
}
if(_str[pos] !='\0')
{
return pos;
}
else
{
return -1;
}


}
size_t Find(const char* str) const
{
assert(str);
int len = strlen(str);
size_t src = 0;
size_t pos = 0;


while (pos + len <= _sz)
{
size_t sub = 0;
src = pos;


while ((_str[src] == str[sub]) && ((int)sub < len))
{
src += 1;
sub += 1;
}


if (sub == len)
return pos;


++pos;
}
return -1;
}
char& operator[](size_t pos) 
{
assert(pos < _sz);
return _str[pos];
}
inline bool operator<(const String& s) const
{
size_t src = 0;
size_t sub = 0;
while((_str[src]!= '\0')&&(s._str[sub]!='\0'))
{
if(_str[src] < s._str[sub])
{
return true;
}
else if(_str[src] > s._str[sub])
{
return false;
}
src++;
sub++;
}
if(_str[src] == '\0')
{
return true;
}
else
{
return false;
}
}
inline bool operator<=(const String& s) const
{
return (_str<s._str)||(_str == s._str);
}
inline bool operator>(const String& s) const
{
return !(_str <= s._str);
}
inline bool operator>=(const String& s) const
{
return (_str>s._str)||(_str == s._str);
}
inline bool operator==(const String& s) const
{
size_t src = 0;
size_t sub = 0;
while ((_str[src] != '\0') && ((s._str[sub] != '\0')))
{
if (_str[src] != s._str[sub])
{
return false;
}
++src;
++sub;
}
if ((_str[src] == '\0') && (s._str[sub] == '\0'))
{
return true;
}
else
{
return false;
}
}
inline bool operator!=(const String& s)const
{
return !(_str == s._str);
}
void Expand(size_t n) 
{
if(n > _capacity)
{
_str = (char*)realloc(_str,n+1);
assert(_str);
_capacity = n;
}
}

private:
char* _str;
size_t _sz;//字符个数
size_t _capacity;//容量
};
原创粉丝点击