文章目录
这篇博客中构造、拷贝构造、析构、还有一些短小频繁调用的函数就不用做函数和定义分离,因为在类中,这种函数会默认是内联函数
string类构造
构造函数有一些容易忽略的细节,我会在下面的代码注释中说明
cpp
#include<iostream>
#include<assert.h>
#include<string.h>
using namespace std;
namespace sg
{
class string
{
public:
string()
//:_str(nullptr)
// 不能这么写,因为当我string对象为空,程序解引用到\0才会终止,遇不到就会崩溃
:_str(new char[1]{'\0'}) // 空间里至少要有一个字符
, _size(0)
, _capacity(0)
{}
//构造函数
string(const char* str)
{
_size = strlen(str);
_capacity = strlen(str);
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
void test_string1();
}
还有另一种写法,不用初始化,直接在构造函数的参数给上缺省值
cpp
/*string()
:_str(new char[1]{'\0'})
, _size(0)
, _capacity(0)
{}*/
//构造函数
//string(string(const char* str = nullptr)
// 如果参数又给空,程序还是会崩,C语言规定常量字符串后面都会有\0,所以直接给空字符串就行
string(const char* str = "")
{
_size = strlen(str);
_capacity = strlen(str);
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
string类拷贝构造
如果使用默认拷贝构造,会引发浅拷贝问题,就是当两个对象指向同一块资源时,释放资源后,会导致一块资源被析构两次,程序会报错。所以自己写一个深拷贝最好
cpp
//拷贝构造函数
//s2(s1)
string(const string& s)// this就是s2
{
_str = new char[s._capacity + 1]; // 开一个和s1一样大的空间
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
string类析构
string的析构和c语言栈实现中的销毁栈大差不差,如果有疑问可以参考这一篇博客
C语言栈和队列
cpp
//析构函数
~string()
{
if (_str) //检查一下_str是否为空,如果为空就不用再释放空间了
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
}
string类运算符重载
string.h
cpp
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
string.cpp
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "practice-string.h"
namespace sg
{
void test_string1()
{
string s1;
string s2("hello world");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
for (size_t i = 0; i < s2.size(); i++)
{
s2[i] += 2;
}
for (auto e : s2)
{
cout << e << " ";
}
cout << endl;
cout << s2.c_str() << endl;
}
}
我通过测试,发现并不能用范围for遍历,报错显示未找到匹配的begin函数,需要我们自己实现
范围for看着很方便,但本质就是迭代器,迭代器模拟的是指针的行为,所以我们返回原生指针即可
string.h
cpp
typedef char* iterator;
typedef 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;
}
赋值运算符
cpp
string operator=(const string& s)
{
if (this != &s)//地址相同,就是自己赋值给自己
{
delete[] _str;
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
string类部分常用接口的模拟实现
string.h
cpp
#pragma once
#include<iostream>
#include<assert.h>
#include<string.h>
using namespace std;
namespace sg
{
class string
{
public:
typedef char* iterator;
typedef 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]{'\0'})
, _size(0)
, _capacity(0)
{}*/
//构造函数
string(const char* str = "")
{
_size = strlen(str);
_capacity = strlen(str);
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
//析构函数
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
//拷贝构造函数
//s2(s1)
string(const string& s)
{
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
const char* c_str() const
{
return _str;
}
size_t size() const
{
return _size;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
// s2 = s1
// s1 = s1
string operator=(const string& s)
{
if (this != &s)//地址相同,就是自己赋值给自己
{
delete[] _str;
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void reserve(size_t n);
void push_back(char ch);
void append(const char* str);
string& operator+=(char ch);
string& operator+=(const char* str);
void insert(size_t pos, char ch);
void insert(size_t pos, const char* str);
void erase(size_t pos, size_t len = npos);
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
string substr(size_t pos = 0, size_t len = npos);
private:
char* _str;
size_t _size;
size_t _capacity;
static const size_t npos;
};
void test_string1();
void test_string2();
bool operator<(const string& s1, const string& s2);
bool operator<=(const string& s1, const string& s2);
bool operator>(const string& s1, const string& s2);
bool operator>=(const string& s1, const string& s2);
bool operator!=(const string& s1, const string& s2);
bool operator==(const string& s1, const string& s2);
ostream& operator<<(ostream& out, const string& s);
istream& operator>>(istream& in, string& s);
}
string.cpp
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
namespace sg
{
const size_t string::npos = -1;//声明和定义分离
void string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void string::push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
string& string::operator+=(char ch)
{
push_back(ch);
return *this;
}
void string::append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
//大于二倍,需要多少开多少,小于二倍,就按二倍扩
reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
}
strcpy(_str + _size, str);//从\0开始拷贝,\0会自动被挤到后面
_size += len;
}
string& string::operator+=(const char* str)
{
append(str);
return *this;
}
void string::insert(size_t pos, char ch)
{
assert(pos < _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
//挪动数据
/*size_t end = _size;
while (end >= 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 string::insert(size_t pos, const char* str)
{
assert(pos < _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t len = strlen(str);
size_t end = _size + len;
while (end >= pos + len)
{
_str[end] = _str[end - len];
--end;
}
for (size_t i = 0; i < len; i++)
{
_str[pos + i] = str[i];
}
_size += len;
}
void string::erase(size_t pos, size_t len)
{
if (len >= _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
for (size_t i = pos + len; i <= _size; i++)
{
_str[i - len] = _str[i];
}
_size -= len;
}
}
size_t string::find(char ch, size_t pos)
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t string::find(const char* str, size_t pos)
{
assert(pos < _size);
const char* ptr = strstr(_str + pos, str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _str;
}
}
string string::substr(size_t pos = 0, size_t len)
{
assert(pos < _size);
if (len > _size - pos)
{
len = _size - pos;
}
string sub;
sub.reserve(len);
for (size_t i = 0; i < len; i++)
{
sub += _str[pos + i];
}
return sub;
}
bool operator<(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) < 0;
}
bool operator<=(const string& s1, const string& s2)
{
return s1 == s2 || s1 < s2;
}
bool operator>(const string& s1, const string& s2)
{
return !(s1 <= s2);
}
bool operator>=(const string& s1, const string& s2)
{
return !(s1 < s2);
}
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
bool operator==(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) == 0;
}
ostream& operator<<(ostream& out, const string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s)
{
s.clear();
//创造一块缓冲
const int N = 256;
char buffer[N];
int i = 0;
char ch;
ch = in.get();
while (ch != ' ' && ch != '\0')
{
buffer[i++] == ch;
if (i == N - 1)// buffer条件
{
buffer[i] = '\0';
s += buffer;
i = 0;
}
if (i > 0)
{
buffer[i] = '\0';
s += buffer;
}
return in;
}
}
void test_string1()
{
string s1;
string s2("hello world");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
for (size_t i = 0; i < s2.size(); i++)
{
s2[i] += 2;
}
cout << s2.c_str() << endl;
}
void test_string2()
{
string s1("hello world");
s1 += '!';
s1 += '#';
cout << s1.c_str() << endl;
s1 += "renqing";
cout << s1.c_str() << endl;
s1.insert(0, '#');
cout << s1.c_str() << endl;
string s2("hello world");
s2.insert(5, "big");
cout << s2.c_str() << endl;
s2.erase(5, 3);
cout << s2.c_str() << endl;
}
}
test.cpp
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
int main()
{
//sg::test_string1();
sg::test_string2();
return 0;
}