#include<iostream>
//string类的基本实现
class String
{
public:
String(int max = 10); //默认构造
String(const char* str); //常量构造
String(const String& str); //拷贝构造
String(int num, char str); //字符构造
void print(); //输出数组
int size(); //获取大小
bool empty(); //判断是否为空串
//重载+=操作符 -- 字符串拼接
String& operator+=(const String& str);
String& operator+=(const char* str);
String& operator+=(char str);
//重载=操作符 -- 字符串赋值
String& operator=(const String& str);
String& operator=(const char* str);
String& operator=(char str);
//重载[]操作符 -- 取值操作
char& operator[](int num);
char& at(int num);
~String(); //析构
private:
char* S; //字符数组
int len; //当前长度
int max; //最大长度 -- 根据最大长度扩容字符数组可以减少时间开销 -- 这里不做实现了
//后续都是按当前长度等于最大长度实现的函数
};
String::String(int num) //默认构造
{
S = new char[num];
S = { 0 };
len = 0;
max = num;
}
String::String(const char* str) //常量初始化
{
char* temp = const_cast<char*>(str); //const cahr* 转 char*
int i = 0;
while (str[i] != '\0'){ i++;}
S = new char[i + 1];
for (int j = 0; j < i; j++) { S[j] = temp[j]; }
S[i] = '\0';
len = i;
max = i;
}
String::String(const String& str) //拷贝构造
{
S = new char[str.len];
for (int i = 0; i < str.len; i++) { S[i] = str.S[i];}
len = str.len;
max = len;
}
String::String(int num, char str) //num个字符初始化
{
S = new char[num + 1];
for (int i = 0; i < num; i++) { S[i] = str; }
S[num] = '\0';
len = num;
max = num;
}
String& String::operator+=(const String& str) //拼接对象
{
char* _temp = new char[len];
char* temp = new char [str.len];
int _num = len;
int num = str.len;
for (int i = 0; i < _num; i++) { _temp[i] = S[i];}
for (int i = 0; i < num; i++) { temp[i] = str.S[i];}
delete[]S;
S = new char[num + _num + 1];
for (int i = 0; i < _num; i++) { S[i] = _temp[i]; }
for (int i = 0; i < num; i++) { S[i + _num] = temp[i]; }
S[num + _num] = '\0';
len = num + _num;
return *this;
}
String& String::operator+=(const char* str) //拼接常量串
{
char* temp = const_cast<char*>(str);
char* _temp = new char[len];
for (int j = 0; j < len; j++) { _temp[j] = S[j]; }
int i = 0;
while (temp[i] != '\0') { i++; }
delete []S;
S = new char[len + i + 1];
for (int j = 0; j < len; j++) { S[j] = _temp[j]; }
for (int j = 0; j < i; j++) { S[j + len] = temp[j]; }
S[len + i] = '\0';
len += i;
max = len;
return *this;
}
String& String::operator+=(char str) //拼接单个字符
{
char* temp = new char[len];
for (int i = 0; i < len; i++) { temp[i] = S[i]; }
delete[]S;
S = new char[len + 1];
for (int i = 0; i < len ; i++) { S[i] = temp[i]; }
S[len] = str;
len += 1;
max = len;
return *this;
}
String& String::operator=(const String& str) //重载赋值运算符
{
char* temp = str.S;
delete[]S;
S = new char[str.len];
for (int i = 0; i < str.len; i++) { S[i] = str.S[i]; }
len = str.len;
return *this;
}
String& String::operator=(const char* str) //常量字符串赋值
{
char* temp = const_cast<char*>(str);
int i = 0;
while (temp[i] != '\0') { i++; }
S = new char[i + 1];
for (int j = 0; j < i; j++) { S[j] = temp[j]; }
len = i;
max = i;
return *this;
}
String& String::operator=(char str) //单个字符赋值
{
S = new char;
S[0] = str;
len = 1;
max = 1;
return *this;
}
char& String::operator[](int num) { return S[num]; } //重载[]运算符
char& String::at(int num) { return S[num]; } //取值操作
bool String::empty() //判断字符串是否为空
{
if (S[0] == '\0') { return true; }
return false;
}
//获取字符串的长度
int String::size() { return len; }
void String::print()
{
if (S == nullptr) { return; }
for (int i = 0; i < len; i++) {std::cout << S[i];}
std::cout << std::endl;
}
String::~String() { delete[]S; S = nullptr; len = 0; max = 0; }
数据结构-字符串(string)(c++)
最新推荐文章于 2024-04-24 14:03:25 发布