文章目录
- string类(三)
- string类的模拟实现:
- 1.默认成员变量和函数
- 2.string的长度和下表引用
- 3.字符串拷贝构造
- 4. +=赋值拷贝
- 5.字符串比较
- 6.字符串的增添操作
- 7.insert插入操作
- 8.遍历字符
string类(三)
string类的模拟实现:
1.默认成员变量和函数
首先自定义构造string类的默认成员变量和函数
声明和定义均在头文件(自己创建的string.h)中进行,主函数仅调用函数
#pragma once
#include <iostream>
using namespace std;
#include <string>
namespace pzh //pzh为命名空间名称
{
class string //pzh::string为自定义的string类的名称
{
public:
string(const char* str = "") //构造函数
: _size(strlen(str))
{
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
~string() //析构函数
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
const char* c_str() //打印字符串函数
{
return _str;
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
void func_str1()
{
string s1;
string s2("hello solity");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
}
}
运行结果:
2.string的长度和下表引用
定义size和**[ ]**:
size_t size() const
{
return _size;
}
char& operator[](size_t pos)
{
assert(pos < _size); //需要引入头文件 #inlcude <assert.h>
return _str[pos];
}
测试功能调用:
void func_str()
{
string s("hello solity");
cout << s.size() << endl;
for (size_t i = 0; i < s.size(); ++i)
{
cout << s[i] << " ";
}
}
运行结果:
3.字符串拷贝构造
此时如果需要进行字符串复制拷贝,便需要进行构造拷贝函数
string(const string& s) //拷贝构造
: _size(s._size) //将s的size长度赋值给成员变量_size
, _capacity(s._capacity)
{
_str = new char[s._capacity + 1]; //加1是给'\0'准备的(深拷贝)
strcpy(_str, s._str);
}
调用测试:
void func_str()
{
string s1("hello solity"); //s
string s2(s1); //this
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
}
运行结果:
4. +=赋值拷贝
赋值不止有这种,当然还有**=**号进行赋值拷贝
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1]; //先开新空间
strcpy(tmp, s._str); //进行拷贝
delete[] _str; //释放旧空间(this)
_str = tmp; //把新开空间给旧空间
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
测试运行:
void func_str()
{
string s1;
string s2("hello solity");
cout << "Before s1 = " << s1.c_str() << endl;
cout << "Before s2 = " << s2.c_str() << endl;
s1 = s2;
cout << "After s1 = " << s1.c_str() << endl;
cout << "After s2 = " << s2.c_str() << endl;
}
输出结果:
5.字符串比较
当需要进行字符串比较的操作时:
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 || *this == s;
}
bool operator<(const string& s) const
{
return !(*this >= s);
}
bool operator<=(const string& s) const
{
return !(*this > s);
}
bool operator!=(const string& s) const
{
return !(*this == s);
}
测试运行:
void func_str()
{
string s1("hello solity");
string s2("hello pzh");
string s3("abc");
cout << (s1 < s2) << endl;
cout << (s1 == s2) << endl;
cout << (s1 >= s2) << endl;
}
输出结果:
6.字符串的增添操作
void reserve(size_t n)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
void push_back(char ch) //添加字符
{
if (_size + 1 > _capacity)
{
reserve(_capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void append(const char* str) //添加字符串
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(len + _size);
}
strcpy(_str + _size, str);
_size += len;
}
测试运行:
void func_str()
{
string s1("hello solity");
s1.push_back(' ');
cout << s1.c_str() << endl;
s1.append("#####################");
cout << s1.c_str() << endl;;
}
输出结果:
当然,追加操作这样就显得很臃肿,我们可以尝试更简介的**+=**操作符来进行:
string& operator+=(const char* str)
{
append(str);
return *this;
}
测试运行:
void func_str()
{
string s1("hello solity");
s1.push_back(' ');
cout << s1.c_str() << endl;
s1.append("#####################");
cout << s1.c_str() << endl;
s1 += "pzh solity";
cout << s1.c_str() << endl;
}
输出结果:
7.insert插入操作
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size + 1 > _capacity)
{
reserve(2 * _capacity);
}
size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
测试运行:
void func_str()
{
string s1("hello solity");
cout << "Before s1 = " << s1.c_str() << endl;
s1.insert(2, 'a');
cout << "After s1 = " << s1.c_str() << endl;
}
输出结果:
8.遍历字符
测试运行:
void func_str()
{
string s1("hello solity");
for (size_t i = 0; i < s1.size(); ++i)
{
cout << s1[i] << " "; //输出运算后结果
}
cout << endl;
for (size_t i = 0; i < s1.size(); ++i)
{
s1[i]++; //对每个字符进行加1操作
}
for (size_t i = 0; i < s1.size(); ++i)
{
cout << s1[i] << " "; //输出运算后结果
}
cout << endl;
}
输出结果:
如果使用范围for进行输出,便需要用到迭代器:
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;
}
测试运行:
void func_str()
{
string s("hello solity");
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto ch : s)
{
cout << ch << " ";
}
}
输出结果:
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
测试运行:
```c++
void func_str()
{
string s("hello solity");
string::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto ch : s)
{
cout << ch << " ";
}
}
输出结果:
综上,完整的string.h头文件如下:
#pragma once
#include <iostream>
using namespace std;
#include <string>
#include <assert.h>
namespace pzh
{
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(const char* str = "") //构造函数
: _size(strlen(str))
{
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
~string() //析构函数
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
const char* c_str() //打印字符串函数
{
return _str;
}
size_t size() const //字符串长度
{
return _size;
}
char& operator[](size_t pos) //字符串下表引用
{
assert(pos < _size);
return _str[pos];
}
string(const string& s) //拷贝构造
: _size(s._size) //将s的size长度赋值给成员变量_size
, _capacity(s._capacity)
{
_str = new char[s._capacity + 1]; //加1是给'\0'准备的(深拷贝)
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;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
delete[] tmp;
}
return *this;
}
//不修改成员变量的函数,保险起见统统加const
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 || *this == s;
}
bool operator<(const string& s) const
{
return !(*this >= s);
}
bool operator<=(const string& s) const
{
return !(*this > s);
}
bool operator!=(const string& s) const
{
return !(*this == s);
}
void reserve(size_t n)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
void push_back(char ch)
{
if (_size + 1 > _capacity)
{
reserve(_capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(len + _size);
}
strcpy(_str + _size, str);
_size += len;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size + 1 > _capacity)
{
reserve(2 * _capacity);
}
size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
void func_str()
{
//...测试功能代码
}
}
main.cpp主文件如下:
#define _CRT_SECURE_NO_WARNINGS
#include "string.h"
int main()
{
pzh::func_str();
return 0;
}