头文件:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<algorithm>
using namespace std;
namespace zgw
{
class string
{
friend ostream& operator<<(ostream& _cout, string& s1);
public:
typedef char* iterator; //重载迭代器
char* begin();
char* end();
string& operator=(string& s);
string(const char*str="");
string(const zgw::string& s1);
~string();
char* c_str()const;
char& operator[](size_t pos);
size_t size()const;
void reserve(size_t n);
void append(const char *s);
void push_back(const char ch);
string& operator+=(const char* a);
string& operator+=(const char ch);
void insert(size_t pos, const char* s);
void insert(size_t pos, const char ch);
void serase(size_t pos=0, size_t len=nopos);
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
void swap(string& s);
string substr(size_t pos = 0, size_t len = nopos);
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;
void clear();
//以下是自己搞的
//void GetCapacity();
//string& operator+=(const char* a);
//string& operator+=(const zgw::string&s1);
//void append(const char*a);
//void append(const string& s1);
private:
static size_t nopos;
char* _str;
size_t _size;
size_t _capacity;
};
istream& operator>> (istream& is, string& str);
ostream& operator<< (ostream& os, const string& str);
ostream& operator<<(ostream& _cout, string& s1);
}
String实现文件:
#include"String.h"
namespace zgw
{
size_t string::nopos = -1;
string& string::operator=(string& s)
{
char* str = new char[s._capacity + 1];
strcpy(str, s._str);
delete[] _str;
_str = str;
_size = s._size;
_capacity = s._capacity;
return s;
}
string::string(const char* str):_size(strlen(str))
{
_str = new char[_size + 1];
strcpy(_str, str);
_capacity = _size;
}
string::string(const string& s1):_size(s1.size())
{
_str = new char[_size+1];
strcpy(_str, s1._str);
_capacity = _size;
}
string::~string()
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
char* string::c_str()const
{
return _str;
}
char& string::operator[](size_t pos)
{
return _str[pos];
}
size_t string::size()const
{
return _size;
}
string::iterator string::begin()
{
return _str;
}
string::iterator string::end()
{
return _str + _size;
}
void string::reserve(size_t n)
{
if (n > _capacity)
{
char* newnode = new char[n + 1];
_capacity = n;
strcpy(newnode, _str);
delete[]_str;
_str = newnode;
}
}
void string::append(const char* s)
{
size_t sz= strlen(s);
if ((sz + _size) > _capacity)
{
size_t newcapa = sz + _size;
reserve(newcapa);
}
strcpy(_str + _size, s);
_size += sz;
}
void string::push_back(const char ch)
{
if (_size == _capacity)
{
size_t newcapa = _capacity + 1;
reserve(newcapa);
}
_str[_size++] = ch;
_str[_size] = 0;
}
string& string::operator+=(const char* a)
{
append(a);
return *this;
}
string& string::operator+=(const char ch)
{
push_back(ch);
return *this;
}
void string::insert(size_t pos, const char* s)
{
assert(pos < _size);
size_t length = strlen(s);
if ((_size + length) > _capacity)
{
reserve(_size + length);
}
int end = _size+length;
while (end > (int)(pos + length-1))
{
_str[end] = _str[end - length];
end--;
}
memcpy(_str + pos, s,length);
}
void string::insert(size_t pos, const char ch)
{
assert(pos < _size);
if (_size == _capacity)
{
size_t newcapa = _capacity + 1;
reserve(newcapa);
}
int end = _size;
while (end >=(int)pos)
{
_str[end + 1] = _str[end];
end--;
}
_str[pos] = ch;
_size++;
}
void string::serase(size_t pos, size_t len)
{
assert(pos < _size);
if (pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + len);
_size -= len;
}
}
size_t string::find(char ch, size_t pos)
{
for (int i = 0; i < _size; i++)
{
if (ch == _str[i])
return i;
}
return nopos;
}
size_t string::find(const char* str, size_t pos)
{
char* s1 = strstr((_str + pos), str);
if (s1 == nullptr)
{
return nopos;
}
return s1 - _str;
}
void string::swap(string& s)
{
std::swap(_str,s._str);
std::swap(_capacity, s._capacity);
std::swap(_size, s._size);
}
string string::substr(size_t pos, size_t len)
{
if (pos + len > _size)
{
string s1(_str + pos);
return s1;
}
string s1;
s1.reserve(len);
for (int i = 0; i < len; i++)
{
s1[i] = _str[i + pos];
}
return s1;
}
bool string::operator<(const string& s) const
{
return strcmp(_str, s._str)<0;
}
bool string::operator>(const string& s) const
{
return !(*this <= s);
}
bool string::operator<=(const string& s) const
{
return (*this) < s || (*this == s);
}
bool string::operator>=(const string& s) const
{
return !((*this) < s);
}
bool string::operator==(const string& s) const
{
return strcmp(_str, s._str);
}
bool string::operator!=(const string& s) const
{
return !(*this == s);
}
void string::clear()
{
_str[0] = 0;
}
//以下是自己搞的
/*ostream& operator<<(ostream& _cout, string& s1)
{
_cout << s1._str ;
return _cout;
}
void string::GetCapacity()
{
_capacity = _capacity * 2;
char* newnode = new char[_capacity * 2];
strcpy(newnode,_str);
for (int i = _size; i < _capacity; i++)
{
newnode[i] = 0;
}
delete[]_str;
_str = newnode;
}
string& string::operator+=(const char* a)
{
while (*a)
{
if (_capacity == _size)
{
GetCapacity();
}
_str[_size++] = *a;
a++;
}
return*this;
}
string& string::operator+=(const string&s1)
{
const char* a = s1.c_str();
while (*a)
{
if (_capacity == _size)
{
GetCapacity();
}
_str[_size++] = *a;
a++;
}
if (_capacity == _size)
{
GetCapacity();
}
return *this;
}*/
//void append(const char* a);
//void append(const string& s1);
istream& operator>> (istream& is, string& str)
{
str.clear();
char ch=is.get();
while (ch != ' ' && ch != '\n')
{
str += ch;
ch = is.get();
}
return is;
}
ostream& operator<< (ostream& os, string& str)
{
for (size_t i = 0; i < str.size(); i++)
{
cout<<str[i];
}
return os;
}
}