废话不说了上代码
String.h
#pragma once
#pragma warning(disable:4996)//去除vs编译器scanf函数报错
#include<iostream>
#include<cstring>
#include<algorithm>
#include<assert.h>
using namespace std;
class String {
public:
//构造函数,开辟空间
String(const char* str = "")
:_str(nullptr)
, _capacity(0) {
_size = strlen(str);
//_str = new char[_size + 1];
Reserve(_size);
strcpy(_str, str);
}
//拷贝构造
String(const String& s)
:_str(nullptr)
, _size(0)
, _capacity(0) {
String tmp(s._str);
Swap(tmp);
}
//析构函数
~String() {
if (_str) {
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
}
//交换函数
void Swap(String& s) {
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
//复制重载
String& operator=( String& s) {
String tmp(s._str);
Swap(tmp);
return *this;
}
//字符串运算重载
String& operator+= (char ch){//字符加
PushBack(ch);
return *this;
}
String& operator+=(const char* str) {//字符串加
Append(str);
return *this;
}
String& operator+=(const String& s) {//String 加
*this += s._str;
return *this;
}
//比较运算符重载
bool operator <(const String& s) {
int flag = strcmp(_str, s._str);
if (flag >= 0) {
return false;
}
else {
return true;
}
}
bool operator ==(const String& s)const {
int flag = strcmp(_str, s._str);
if (flag != 0) {
return false;
}
return true;
}
bool operator !=(const String& s)const {
if (*this == s)
return false;
return true;
// return ~(*this == s);
}
bool operator <=(const String& s) {
if((*this<s)||(*this==s))
return true;
return false;
}
bool operator >(const String& s) {
if ((*this) <= s)
return false;
return true;
}
bool operator >=(const String& s) {
if (*this < s) {
return false;
}
return true;
}
//末尾加字符
void PushBack(char ch) {
Insert(_size, ch);
}
//末尾加字符串
void Append(const char* str) {
Insert(_size, str);
}
//指定位置加字符
void Insert(size_t pos, char ch) {
assert(pos <= _size);
if (_size == _capacity) {
Reserve(_capacity * 2);
}
//注意判断条件,当判断条件为>=零时 pos为零,会出错
for (size_t i = _size+1; i >= pos+1; --i) {
_str[i ] = _str[i-1];
}
_str[pos] = ch;
_size++;
}
//指定位置加字符串
void Insert(size_t pos, const char* str) {
assert(pos <= _size);
int len = strlen(str);
if (_size + len >= _capacity) {
Reserve(_size+len);
}
//借用字符插入函数的方法,但是时间复杂度高。
/*for (size_t i = 0; i < len; i++) {
Insert(i+ pos, str[i]);
}
_str[_size] = '\0';*/
//在此处要注意判断条件不能>=0 因为size_t是无符号整型,没有负数
_size += len;
for (size_t i = _size; i >= pos+len; --i) {
_str[i] =_str[i-len];
}
for (size_t i = pos; i < pos+len; ++i) {
_str[i] = str[i - pos];
}
}
//扩容而已_capacity
void Reserve(size_t n) {
if (n == 0 || n > _capacity) {
size_t newsize = n;
//采用加值清尾法,使已给值到达下一个8的倍数,清尾后,则正好符合整数8的倍数
if (n % 8 != 0)
newsize = (n + 7) & (~7);
else
newsize = n + 8;
char* newstr = new char[newsize];
if (_str)
strcpy(newstr, _str);
_str = newstr;
_capacity = newsize - 1;
}
}
//扩容并增加_size大小
void Resize(size_t n, char ch = '\0') {
if (n <= _size) {
_size = n;
_str[_size] = '\0';
}
else {
Reserve(n);
for (size_t i = _size; i < n; ++i) {
_str[i] = ch;
++_size;
}
_str[_size] = '\0';
}
}
//输出函数
void display() {
cout << _str << " " << _size << " " << _capacity << endl;
}
friend istream& operator>>(istream& in, String& s);
friend ostream& operator<<(ostream& out,String& s);
private:
char* _str;
size_t _size;
size_t _capacity;
};
istream& operator>>(istream& in, String& s) {
cin >> s._str;
return in;
}
ostream& operator<<(ostream& out, String& s) {
out << s._str << " " << s._size << " " << s._capacity << endl;
return out;
}
main.cpp
#include <iostream>
#include"String.h"
//输入输出和构造函数,赋值的测试
void Test1() {
String s1 = "abc";
String s2(s1);
String s3 = s1;
cout << s1;
cout << s2;
cout << s3;
s1.display();
s2.display();
s3.display();
}
//插入函数 字符和字符串 的测试
void Test2() {
String s1 = "abcdg";
s1.display();
String s2(s1);
s2.display();
String s3 = s1;
s3.display();
s1.Insert(0,'t');//测试字符插入
s2.Insert(0, "qwer");//测试字符串插入
s3.Insert(3, "xyz");//插入尾部
s1.display();
s2.display();
s3.display();
}
//对于字符 字符串 String对象的+=运算符测试
void Test3() {
String s1 = "abc";
String s2(s1);
String s3 = s1;
s1 += 'z';
s2 += "xyz";
s3 += s2;
s1.display();
s2.display();
s3.display();
}
//对比较运算符的重载
void Test4() {
String s1 = "abc";
String s4 = s1;
String s5 = "a";
String s2(s1);
String s3 = s1;
s2 += "xyz";
s3 += s2;
cout << (s1 != s2) << endl;
cout << (s1 != s3) << endl;
cout << (s1 != s4) << endl;
cout << (s1 != s5) << endl;
cout << "--------------------" << endl;
cout << (s1 <= s2) << endl;
cout << (s1 < s2) << endl;
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
cout << (s1 >= s2) << endl;
cout << (s1 >s3) << endl;
cout << "-----------------" << endl;
cout << (s1 < s2) << endl;
cout << (s1 < s3) << endl;
cout << (s2 < s3) << endl;
cout << (s1 == s2) << endl;
cout << (s1 ==s3) << endl;
cout << (s2 ==s3) << endl;
s1.display();
s2.display();
s3.display();
}
int main()
{
/*String s1 = "abc";
String s2(s1);
cout << s1 << endl;
cout << s2 << endl;
//s1 += 'a';
s1.Insert(1, 'a');
//s2.Insert(1, "qwerty");
//s2 += "cbajsldfalskdflkasjldfjlksdf";
cout << s1 << endl;
s2.display();
s2 = s1;
s2.display();
//s2.display();
//s2.Reserve(100);
//s2.display();
//s2.Resize(150);
//s2.display();
//cout << s2 << endl;
*/
Test4();
return 0;
}