字符串类的增删查改
class String
{
public:
String(const char* str = "");
~String();
String(const String& s);
const String& operator=(const String& s);
void Swap(String& s);
char* GetStr();
size_t Len();
size_t Capacity();
void PushBack(char ch);
void PushBack(const 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;
char& operator[](size_t pos);
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;
private:
char* _str;
size_t _len;
size_t _capacity;
};
class_string.h
#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
class String
{
public:
String(const char* str = "")
:_len(strlen(str)), _capacity(_len)
{
_str = (char*)malloc(_len + 1);
strcpy(_str, str);
}
~String()
{
free(_str);
}
String(const String& s)
:_len(s._len), _capacity(s._capacity)
{
_str = (char*)malloc(s._capacity + 1);
strcpy(_str, s._str);
}
const String& operator=(const String& s);
void Swap(String& s);
char* GetStr();
size_t Len();
size_t Capacity();
void PushBack(char ch);
void PushBack(const 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;
char& operator[](size_t pos);
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;
private:
char* _str;
size_t _len;
size_t _capacity;
};
string.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "class_string.h"
const String& String::operator=(const String& s)
{
if (_capacity < s._len)
{
_len = s._len;
_str = (char*)realloc(_str, _len + 1);
_capacity = _len;
}
strcpy(_str, s._str);
return *this;
}
void String::Swap(String& s)
{
swap(_str, s._str);
swap(_len, s._len);
swap(_capacity, s._capacity);
}
char* String::GetStr()
{
return _str;
}
size_t String::Len()
{
return _len;
}
size_t String::Capacity()
{
return _capacity;
}
void String::PushBack(char ch)
{
if (_len == _capacity)
{
_str = (char*)realloc(_str, ++_len + 1);
_capacity++;
}
_str[_len] = '\0';
_str[_len - 1] = ch;
}
void String::PushBack(const char* str)
{
_len += strlen(str);
if (_len > _capacity)
{
_str = (char*)realloc(_str, _len + 1);
_capacity = _len;
}
char* dest = _str;
while (*dest)dest++;
while (*dest++ = *str++);
}
void String::PopBack()
{
_str[--_len] = '\0';
}
void String::Insert(size_t pos, char ch)
{
assert(pos <= _len);
if (_len == _capacity)
{
_str = (char*)realloc(_str, ++_len + 1);
_capacity = _len;
}
int dest = _len;
while (dest > pos)
{
_str[dest] = _str[dest - 1];
dest--;
}
_str[pos] = ch;
}
void String::Insert(size_t pos, const char* str)
{
if (pos > _len)
return;
int len = strlen(str);
_len += len;
if (_len > _capacity)
{
_str = (char*)realloc(_str, _len + 1);
_capacity = _len;
}
int i = 0;
while (_len - i > pos + len - 1)
{
_str[_len - i] = _str[_len - i - len];
i++;
}
int j = 1;
while (_len - i - j + 2 >= pos + 1)
{
_str[_len - i - j + 1] = str[len - j];
j++;
}
}
void String::Erase(size_t pos, size_t count)
{
if (pos >= _len)
return;
if (pos + count >= _len)
{
_str[pos] = '\0';
_len = pos;
}
else
{
while (pos + count <= _len)
{
_str[pos] = _str[pos + count];
pos++;
}
_len -= count;
}
}
int String::Find(char ch) const
{
int ret = 0;
while (_str[ret] != ch&&_str[ret] != '\0')
ret++;
return _str[ret] == '\0' ? -1 : ret;
}
int String::Find(const char* str) const
{
if (*str == '\0')
return _len;
int ret = 0;
size_t len = strlen(str);
while (ret + len <= _len)
{
int i = 0;
while (_str[ret + i] == str[i] && str[i] != '\0')
i++;
if (str[i] == '\0')
return ret;
ret++;
}
return -1;
}
char& String::operator[](size_t pos)
{
return _str[pos];
}
bool String::operator==(const String& s) const
{
if (_len == s._len)
{
int i = 0;
while (_str[i] == s._str[i])
{
if (_str[i] == '\0')
return true;
i++;
}
}
return false;
}
bool String::operator<(const String& s) const
{
int i = 0;
while (_str[i] == s._str[i])
{
if (_str[i] == '\0')
return false;
i++;
}
return _str[i] - s._str[i] < 0 ? true : false;
}
bool String::operator<=(const String& s) const
{
int i = 0;
while (_str[i] == s._str[i])
{
if (_str[i] == '\0')
return true;
i++;
}
return _str[i] - s._str[i] < 0 ? true : false;
}
bool String::operator>(const String& s) const
{
int i = 0;
while (_str[i] == s._str[i])
{
if (_str[i] == '\0')
return false;
i++;
}
return _str[i] - s._str[i] > 0 ? true : false;
}
bool String::operator>=(const String& s) const
{
int i = 0;
while (_str[i] == s._str[i])
{
if (_str[i] == '\0')
return true;
i++;
}
return _str[i] - s._str[i] > 0 ? true : false;
}
bool String::operator!=(const String& s)const
{
int i = 0;
while (_str[i] == s._str[i])
{
if (_str[i] == '\0')
return false;
i++;
}
return true;
}