本文将string类中一些主要的函数模拟出来!
通过缺省值构造函数,可以同时解决默认构造函数
string( const char* str="")
:_str(new char[strlen(str)+1])
,_size(strlen(str))
,_capacity(strlen(str))
{
strcpy(_str, str);
}
这里设计深浅拷贝,如果直接调用默认拷贝构造的话就是浅拷贝,当调用析构函数的时候就会崩溃,原因就是char*指针指向同一块空间了,所以我们必须采取深拷贝的方法,重新开辟一块空间
string(const string& s)
{
_str=new char[s._capacity+1];
_size = s._size;
_capacity = s._capacity;
strcpy(_str, s._str);
}
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char(s._capacity + 1);
strcpy(tmp, s._str);
delete[] _str;
_size = s._size;
_capacity = s._capacity;
_str = tmp;
}
return *this;
}
通过调用构造函数完成拷贝构造!!
void swap(string& tmp)
{
std::swap(_str, tmp._str);
std::swap(_size, tmp._size);
std::swap(_capacity, tmp._capacity);
}
string(const string& s)
{
string tmp(s._str);//调用构造函数
swap(tmp);
}
之前我们都是用的引用传参,现在我们用传值传参,我知道传值传参会生成一个拷贝,所以直接让s代替tmp打工
string& operator=(string s)
{
swap(s);
return *this;
}
string& operator=(const string& s)
{
if (this != &s)
{
string tmp(s);//调用拷贝构造
swap(tmp);
}
return *this;
}
~string()
{
_size = 0;
_capacity = 0;
_str = nullptr;
delete[] _str;
}
size_t size() const
{
return _size;
}
char* c_str()
{
return _str;
}
size_t capacity() const
{
return _capacity;
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
char& operator[](size_t pos)
{
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 push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
void append(const string& s)
{
if (_size + s._size > _capacity)
{
reserve(_size + s._size);
}
strcpy(_str + _size, s._str);
_size += s._size;
}
string& operator+=(const string& s)
{
append(s);
return *this;
}
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size+1 == _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;
return *this;
}
string& insert(size_t pos, const string& s)
{
assert(pos <= _size);
if (_size+s._size == _capacity)
{
reserve(_size + s._size);
}
size_t end = _size + s._size;
while (end > pos)
{
_str[end] = _str[end - s._size];
--end;
}
strncpy(_str + pos, s._str, s._size);
_size+=s._size;
return *this;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
size_t find(char ch, size_t pos = 0) const
{
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* n = strstr(_str + pos, sub);
if (n == nullptr)
return npos;
else
return n - _str;
}
string substr(size_t pos, size_t len = npos) const
{
assert(pos < _size);
size_t realLen = len;
if (len == npos || pos + len > _size)//防止len过大
{
realLen = _size - pos;
}
string sub;
for (size_t i = 0; i < realLen; ++i)
{
sub += _str[pos + i];
}
return sub;
}
bool operator>(const string& s) const
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string& s) const
{
return strcmp(_str, s._str) == 0;
}
bool operator>=(const string& s) const
{
return strcmp(_str, s._str) >= 0;
}
bool operator<=(const string& s) const
{
return strcmp(_str, s._str) <= 0;
}
bool operator<(const string& s) const
{
return strcmp(_str, s._str) < 0;
}
bool operator!=(const string& s) const
{
return !(*this==s);
}
这里我并没有采用友元函数,直接通[]取到对应的字符
流插入这边做了一个小小的优化,就是我们每次输入都得+=,这用频繁的开辟空间效率较低,我们采用一个临时数组,将我们输入的这些字符都放到临时数组里然后统一+=到字符串中
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)
{
s.clear();
char ch;
ch = in.get();
const size_t N = 32;
char buff[N];
size_t i = 0;
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == N - 1)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
buff[i] = '\0';
s += buff;
return in;
}
#pragma once
#include
#include
using std::istream;
using std::ostream;
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str+_size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
//string()
// :_str(new char[1])
// ,_size(0)
// ,_capacity(0)
//{
// _str[0]='\0';
//}
string( const char* str="")
:_str(new char[strlen(str)+1])
,_size(strlen(str))
,_capacity(strlen(str))
{
strcpy(_str, str);
}
//传统写法
//string(const string& s)
//{
// _str=new char[s._capacity+1];
// _size = s._size;
// _capacity = s._capacity;
// strcpy(_str, s._str);
//}
//string& operator=(const string& s)
//{
// if (this != &s)
// {
// char* tmp = new char(s._capacity + 1);
// strcpy(tmp, s._str);
// delete[] _str;
// _size = s._size;
// _capacity = s._capacity;
// _str = tmp;
// }
// return *this;
//}
//老板思维(1)
void swap(string& tmp)
{
std::swap(_str, tmp._str);
std::swap(_size, tmp._size);
std::swap(_capacity, tmp._capacity);
}
string(const string& s)
{
string tmp(s._str);//调用构造函数
swap(tmp);
}
//string& operator=(const string& s)
//{
// if (this != &s)
// {
// string tmp(s);
// swap(tmp);
// }
// return *this;
//}
//老板思维(2)
//让s代替tmp打工
string& operator=(string s)
{
swap(s);
return *this;
}
~string()
{
_size = 0;
_capacity = 0;
_str = nullptr;
delete[] _str;
}
size_t size() const
{
return _size;
}
char* c_str()
{
return _str;
}
size_t capacity() const
{
return _capacity;
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
char& operator[](size_t pos)
{
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 push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
void append(const string& s)
{
if (_size + s._size > _capacity)
{
reserve(_size + s._size);
}
strcpy(_str + _size, s._str);
_size += s._size;
}
string& operator+=(const string& s)
{
append(s);
return *this;
}
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size+1 == _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;
return *this;
}
string& insert(size_t pos, const string& s)
{
assert(pos <= _size);
if (_size+s._size == _capacity)
{
reserve(_size + s._size);
}
size_t end = _size + s._size;
while (end > pos)
{
_str[end] = _str[end - s._size];
--end;
}
strncpy(_str + pos, s._str, s._size);
_size+=s._size;
return *this;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
size_t find(char ch, size_t pos = 0) const
{
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* n = strstr(_str + pos, sub);
if (n == nullptr)
return npos;
else
return n - _str;
}
string substr(size_t pos, size_t len = npos) const
{
assert(pos < _size);
size_t realLen = len;
if (len == npos || pos + len > _size)//防止len过大
{
realLen = _size - pos;
}
string sub;
for (size_t i = 0; i < realLen; ++i)
{
sub += _str[pos + i];
}
return sub;
}
bool operator>(const string& s) const
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string& s) const
{
return strcmp(_str, s._str) == 0;
}
bool operator>=(const string& s) const
{
return strcmp(_str, s._str) >= 0;
}
bool operator<=(const string& s) const
{
return strcmp(_str, s._str) <= 0;
}
bool operator<(const string& s) const
{
return strcmp(_str, s._str) < 0;
}
bool operator!=(const string& s) const
{
return !(*this==s);
}
private:
char* _str;
int _size;
int _capacity;
const static size_t 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)
{
s.clear();
char ch;
ch = in.get();
const size_t N = 32;
char buff[N];
size_t i = 0;
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == N - 1)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
buff[i] = '\0';
s += buff;
return in;
}