stringクラス[],==などのメンバー関数
4673 ワード
#include <iostream>
using namespace std;
class String
{
public:
String(char* str = "")
:_str(new char[strlen(str) + 5]),
_size(strlen(str)),
_capacity(strlen(str)+5)
{
*(int*)(_str) = 1;
_str += 4;
strcpy(_str, str);
}
String(const String& s)
:_str(s._str)
{
++*(int*)(_str-4);
}
String& operator=(String& s)
{
if (this != &s)
{
Delete();
_str = s._str;
++(*(int*)(_str - 4));
}
return *this;
}
void Delete()
{
if (--*(int *)(_str - 4) == 0)
{
cout << *(int *)(_str - 4) << endl;
_str -= 4;
delete[] _str;
}
}
void _CheckCapacity(size_t capacity)
{
if (_capacity < capacity)
{
char *tmp = new char[capacity*2];
*(int *)tmp = 1;
tmp += 4;
strcpy(tmp,_str);
_str -= 4;
delete[] _str;
_str = tmp;
_capacity = capacity * 2;
}
}
~String()
{
Delete();
}
void Display()
{
cout <<_str<< endl;
//cout << *(int *)(_str - 4) << endl;
//cout << _capacity << endl;
//cout << _size << endl;
}
void PushBack(char s)
{
size_t capacity = _capacity + 1;
_CheckCapacity(capacity);
_str[_size++] = s;
_str[_size] = '\0';
}
void PushBack(char * s)
{
size_t capacity =_size + strlen(s) + 1;
_CheckCapacity(capacity);
strcat(_str, s);
_size = _size + strlen(s);
}
char* Find(const char *target)const
{
int i, j, k;
char* s = _str;
int length_Str = strlen(_str);
int lengthTarget = strlen(target);
if (lengthTarget > length_Str||_str==NULL)
{
return NULL;
}
for (i = 0; i < length_Str; i++)
{
for (j = i, k = 0; j < length_Str;j++)
{
if (target[k] && target[k] == _str[j])
{
k++;
}
else
break;
if (target[k] == '\0')
{
return _str + i;
}
}
}
return NULL;
}
void Insert(size_t position, char c)
{
if (position<1 || position >(_size + 1))
{
cout << " " << endl;
return;
}
size_t capacity = _size + 2;
_CheckCapacity(capacity);
char* temp = new char[capacity];
if (position == 1)
{
temp[0] = c;
temp[1] = '\0';
strcat(temp, _str);
}
else
{
strncpy(temp, _str, position - 1);
temp[position - 1] = c;
temp[position] = '\0';
strcat(temp, _str + position - 1);
}
strcpy(_str, temp);
delete[] temp;
}
void Insert(size_t position, const char* target)
{
if (position<1||position >( _size+1))
{
cout << " " << endl;
return;
}
size_t capacity = _size + strlen(target) + 1;
_CheckCapacity(capacity);
char* temp = new char[capacity];
if (position == 1)
{
strcpy(temp, _str);
strcat(temp, target);
}
else
{
strncpy(temp, _str, position - 1);
temp[position - 1] = '\0';
strcat(temp, target);
strcat(temp, _str + position - 1);
}
strcpy(_str, temp);
delete[] temp;
}
void PopBack()
{
if (_size == 0)
{
cout << " " << endl;
return;
}
_str[_size-1] = '\0';
--_size;
}
char operator[](size_t index)
{
if (index<0 || index>_size - 1)
{
return NULL;
}
return _str[index];
}
bool operator<(const String& s)
{
char *s1 = _str;
char *s2 = s._str;
while (*s1 != '\0' &&*s2 != '\0' &&*s1 ==* s2)
{
s1++;
s2++;
}
if (*s1 - *s2 < 0)
{
return true;
}
else
{
return false;
}
}
bool operator<=(const String& s)
{
return *this < s || *this == s;
}
bool operator>(const String& s)
{
return !(*this <= s);
}
bool operator>=(const String& s)
{
return !(*this < s);
}
bool operator==(const String& s)
{
char *s1 = _str;
char *s2 = s._str;
while (*s1 != '\0'&&*s2 != '\0'&&*s1 ==* s2)
{
s1++;
s2++;
}
if ((*s1 =='\0')&&(*s2 == '\0'))
return true;
else
return false;
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
void Test1()
{
/*String s1("abc");
String s2(s1);
s2.Display();
String s3;
s3.Display();
s3 = s1;
s3.Display();*/
}
void Test2()
{
/*String s1("ab");
s1.PushBack('c');
s1.Display();
s1.PushBack("abcHHHHHHHHHHHHHHHHHHHHHHHHHd");
s1.Display();*/
}
void Test3()
{
/*String s1 = "abcdef";
s1.Insert(3, "ajjhj");
s1.Display();*/
String s1("abc");
//cout << s1[3] << endl;
s1.PopBack();
s1.Display();
}
int main()
{
Test3();
system("pause");
return 0;
}