一、默认成员函数
1、构造函数
为了更方便使用,我们直接写一个全缺省的构造函数。
这里_str不能等于空指针,若是空指针,1对其解引用就会报错,也不能是空格,因为字符串初始时不一定是空格,这里可以是"\0"。
string(const char* str="")
: _size(strlen(str))
, _capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
2、析构函数
~string()
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
3、拷贝构造
这里不能是浅拷贝,因为这里涉及到资源管理的问题,浅拷贝会造成析构两次和同时修改的的问题,因此必须自己实现拷贝构造函数,进行深拷贝。
这里展示两种写法
//传统写法:
/*string(const string& s)
{
_str = new char[s._capacity];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}*/
//现代写法:
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s. _size);
std::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
, _size(0)
, _capacity(0)
{
string tmp(s._str);
swap(tmp);
}
4、赋值重载
默认的赋值重载函数会将原有对象的所有成员变量一一赋值给新对象,类似于默认生成的拷贝构造函数
//赋值重载,s2=s3
/*string& operator=(const string& s)
{
if (*this != s)
{
char* tmp = new char[s._capacity + 1];
strcmp(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}*/
//现代写法:
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s. _size);
std::swap(_capacity, s._capacity);
}
string& operator=(string tmp)
{
swap(tmp);
return *this;
}
(二)容量相关
1、size
size_t size() const
{
return _size;
}
2、capacity
size_t capacity() const
{
return _capacity;
}
3、reverve
功能是更要字符串的容量
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
4、resize
功能是修改字符串有效字符数,大体上可以分为插入数据和删除数据,插入数据可以用'\0'。
void resize(size_t n, char ch = '\0')
{
if (n <= _size)
{
_str[n] = '\0';
_size = n;
}
else
{
reserve(n);
while (_size<n)
{
_str[_size] = ch;
_size++;
}
}
}
5、clear
功能:清除字符串的内容,但是不释放字符串的空间
void clear()
{
_str[0] = '\0';
_size = 0;
}
(三)对元素进行操作
1、operator[]
功能:对元素进行访问
char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
2、append()
功能:追加字符串
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
3、push_back()
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
4、insert()
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size + 1;//方案二
while (end >= pos)//方案一:强转
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
_size++;
}
void insert(size_t pos, const char* ch)
{
assert(pos <= _size);
size_t len = strlen(ch);
if (_capacity < len + _size)
{
reserve(len + _size);
}
size_t end = _size;
while (end>=(int)pos)
{
_str[end + len] = _str[end];
end--;
}
strncpy(_str + pos, ch, len);
_size += len;
}
5、erase()
功能:删除字符串中的内容
void erease(size_t pos, size_t len = npos)
{
assert(pos <= _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t begin = pos + len;
while (begin<=_size)
{
_str[begin - len] = _str[begin];
begin++;
}
_size -= len;
}
}
(四)find系列
1、find
功能是查找字符串中的内容
size_t find(char ch, size_t pos = 0)
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* sub, size_t pos = 0)
{
const char* p = strstr(_str + pos, sub);
if (p)
{
return p - _str;
}
else
{
return npos;
}
}
2、substr
string substr(size_t pos,size_t len=npos)
{
string s;
size_t end = pos + len;
if (len == npos || pos + len >= s._size)
{
len = _capacity;
end = _size;
}
s.reserve(len);
for (size_t i = pos; i < end; i++)
{
s += _str[i];
}
return s;
}
(五)运算符重载
1、operator<
bool operator<(const string& s) const
{
return strcmp(_str, s._str) < 0;
}
2、 operator==
bool operator==(const string& s)
{
return strcmp(_str, s._str) == 0;
}
3、operator<=
bool operator<=(const string& s)
{
return *this < s || *this == s;
}
4、operator>
bool operator>(const string& s)
{
return !(*this <= s);
}
5、operator>=
bool operator>=(const string& s)
{
return !(*this < s);
}
6、 operator!=
bool operator!=(const string& s)
{
return !(*this == s);
}
全部代码
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace My_STL
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
const static size_t npos;
iterator begin()
{
return _str;
}
const_iterator begin() const
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator end() const
{
return _str + _size;
}
string(const char* str="")
: _size(strlen(str))
, _capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
//拷贝构造s2(s1)
//传统写法:
/*string(const string& s)
{
_str = new char[s._capacity];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}*/
//赋值重载,s2=s3
/*string& operator=(const string& s)
{
if (*this != s)
{
char* tmp = new char[s._capacity + 1];
strcmp(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}*/
//现代写法:
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s. _size);
std::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
, _size(0)
, _capacity(0)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(string tmp)
{
swap(tmp);
return *this;
}
~string()
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0')
{
if (n <= _size)
{
_str[n] = '\0';
_size = n;
}
else
{
reserve(n);
while (_size<n)
{
_str[_size] = ch;
_size++;
}
}
}
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* ch)
{
append(ch);
return *this;
}
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size + 1;//方案二
while (end >= pos)//方案一:强转
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
_size++;
}
void insert(size_t pos, const char* ch)
{
assert(pos <= _size);
size_t len = strlen(ch);
if (_capacity < len + _size)
{
reserve(len + _size);
}
size_t end = _size;
while (end>=(int)pos)
{
_str[end + len] = _str[end];
end--;
}
strncpy(_str + pos, ch, len);
_size += len;
}
void erease(size_t pos, size_t len = npos)
{
assert(pos <= _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t begin = pos + len;
while (begin<=_size)
{
_str[begin - len] = _str[begin];
begin++;
}
_size -= len;
}
}
bool operator<(const string& s) const
{
return strcmp(_str, s._str) < 0;
}
bool operator==(const string& s)
{
return strcmp(_str, s._str) == 0;
}
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)
{
return !(*this == s);
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
const char* c_str()
{
return _str;
}
size_t find(char ch, size_t pos = 0)
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* sub, size_t pos = 0)
{
const char* p = strstr(_str + pos, sub);
if (p)
{
return p - _str;
}
else
{
return npos;
}
}
string substr(size_t pos,size_t len=npos)
{
string s;
size_t end = pos + len;
if (len == npos || pos + len >= s._size)
{
len = _capacity;
end = _size;
}
s.reserve(len);
for (size_t i = pos; i < end; i++)
{
s += _str[i];
}
return s;
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
const size_t string::npos = -1;
ostream& operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); i++)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
char ch;
//cin >> ch;这样拿不到空格
ch = in.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}
return in;
}
void test_mt_string()
{
string s1;
cin >> s1;
cout << s1 << endl;
}
}