模拟实现string类,先定义一个string类。
//为了和标准库中的string进行区分,我们自定义一个命名空间MyString,将自己实现的string类写到Mystring命名空间里面
namespace MyString
{
class string
{
public:
private:
char* _str;
size_t _size;
size_t _capacity;
};
}
1.对string类对象的基本操作
namespace MyString
{
class string
{
public:
typedef char* iterator;//迭代器
typedef const char* const_iterator;
const_iterator begin()const
{
return _str;
}
const_iterator end()const
{
return _str + _size;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const char* c_str()
{
return _str;
}
string(const char* str = "")
:_size(strlen(str))
{
_capacity = _size;
_str = new char[_capacity + 1];
}
//无参构造函数
//
//string()
// :_str(new char[1]{'\0'})
// ,_size(0)
// ,_capacity(0)
//{
//}
//拷贝构造函数
/*string(const string& s)
{
_str = new char[s._capacity + 1];
_size = s._size;
_capacity = s._capacity;
}*/
/*string(const string& s)
{
string tmp(s._str);
swap(tmp);
}*/
/*string& operator=(string tmp)
{
swap(tmp);
return *this;
}
void swap(string& s)
{
swap(_str, s._str);
}
*/
string& operator=(string& s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_capacity = s._capacity;
_size = s._size;
return *this;
}
//析构函数
~string()
{
delete[]_str;
_capacity = 0;
_size = 0;
}
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos)const
{
assert(pos < _size);
return _str[pos];
}
//将有效字符串的个数改为n个
void resize(size_t n, char ch = '\0')
{
if (n < _size)
{
_str[n] = '\0';
_size = n;
}
else
{ //判断是否需要扩容
reserve(n);
for (size_t i = _size; i < n; i++)
{
_str[i] = ch;
}
_str[n] = '\0';
_size = n;
}
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n+1];
strcpy(tmp, _str);
delete[]_str;
_capacity = n;
}
}
void push_back(char ch)
{
if (_size = _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
_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);
delete[]str;
_size += len;
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
delete[]str;
_size += len;
return *this;
}
//插入单个字符
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
//无符号类型不会小于零 可能会发生程序奔溃
/*size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
end--;
}*/
//解决方案一: 强制类型转换
/*int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
end--;
}*/
//采用第二种方案修改区间
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
}
//插入字符串
void insert(size_t pos, char* str)
{
assert(pos < _size);
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
}
strncpy(_str + pos, str,len);
delete[]str;
_size += len;
}
//删除从pos位置开始,长度为len的字符
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || len >= _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
//查找 -- 返回找到字符或者字符串位置的下标
size_t find(char ch,size_t pos=0)const
{
assert(pos < _size);
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
{
assert(pos < _size);
const char* p = strstr(_str + pos, sub);
if (p)
{
return p - _str;
}
else
{
return npos;
}
}
//截取一段子串,返回字符串对象
string substr(size_t pos = 0,size_t len = npos)
{
string sub;
if (len >= _size - pos)
{
for (size_t i = pos; i < _size; i++)
{
sub += _str[i];
}
}
else
{
for (size_t i = pos; i < pos + len; i++)
{
sub += _str[i];
}
}
return sub;
}
bool operator>(const string& s)
{
return strcmp(_str, s._str) > 0;
}
bool operator<(const string& s)
{
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 || *this == s;
}
bool operator!=(const string& s)
{
return !(*this == s);
}
//[]访问操作符
const char operator[](size_t i) const
{
return _str[i];
}
char operator[](size_t i)
{
return _str[i];
}
//返回string大小
size_t size() const
{
return _size;
}
//返回内存空间大小
size_t capacity() const
{
return _capacity;
}
//返回C格式的字符串
const char* c_str() const
{
return _str;
}
private:
char* _str=nullptr;
size_t _size=0;
size_t _capacity=0;
public:
static const int npos;
};
}
测试端口:
void test_string1()
{
/*string str("hello world");
cout << str.c_str() << endl;*/
string s1;
cout << s1.c_str() << endl;
string s2("hello world");
for (size_t i = 0; i < s2.size(); i++)
{
cout << s2[i] << " ";
}
cout << endl;
string::iterator it = s2.begin();
while (it != s2.end())
{
cout << *it << " ";
it++;
}
cout << endl;
for (auto ch : s2)
{
cout << ch << " ";
}
}
void test_string2()
{
string s1;
s1.push_back('#');
cout << s1.c_str() << endl;
string s2;
s2.append("hello world");
cout << s2.c_str() << endl;
}
void test_string3()
{
string s1("hello world");
s1.insert(0, '#');
cout << s1.c_str() << endl;
s1.insert(6, "%%20");
cout << s1.c_str() << endl;
s1.erase(0, 6);
cout << s1.c_str() << endl;
s1.erase(1);
cout << s1.c_str() << endl;
}
void test_string4()
{
string s1("hello world");
cout << s1 << endl;
s1.resize(5);
cout << s1 << endl;
s1.resize(20, '#');
cout << s1 << endl;
}
void test_string5()
{
//如何拿到一个文件的后缀
string s1("test.cpp");
size_t i = s1.find('.');//找到.位置,并返回下标
string ret1 = s1.substr(i);//从下标为i位置开始,截取到s1末尾
cout << ret1 << endl; //打印.cpp
//https://legacy.cplusplus.com/reference/string/
//如何根据以上网站分割出协议、域名、资源名
string s3("https://legacy.cplusplus.com/reference/string/");
size_t x = s3.find(':');
string ret3 = s3.substr(0, x);
size_t y = s3.find('/', x + 3);
string ret4 = s3.substr(x + 3, y - (x + 3));
string ret5 = s3.substr(y);
cout << ret3 << endl;
cout << ret4 << endl;
cout << ret5 << endl;
}
void test_string6()
{
string s1("hello world");
string s2;
s2 = s1;
cout << s2 << endl;
}
void test_string7()
{
string s1;
cin >> s1;
cout << s1;
}
2.浅拷贝
浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为还有效,所以当继续对资源进项操作时,就会发生发生了访问违规。
3.深拷贝
如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。
在堆区重新开辟一片内存保存数据,这样就可以避免内存重复释放!
4.写时拷贝
浅拷贝会给我们带来很多的问题,例如:
1.浅拷贝对同一块空间会释放两次(调用两次析构函数);
2.一个数据修改会影响到另一个数据。
那么我们解决浅拷贝的方法就只有深拷贝吗?当然不是,我们接下来要介绍的写实拷贝也可以解决。
深拷贝在共享资源的基础上,进行拷贝多次,会导致空间资源上的浪费。避免一些空间浪费和减少拷贝操作的技术叫做写时拷贝技术。