-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathString.h
148 lines (146 loc) · 3.38 KB
/
String.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//Date : 2016.11.6
//Autore : yqtao
/*
string 类的实现
*/
#ifndef STRING_H
#define STRING_H
#include<iostream>
using namespace std;
class String {
private:
char* _data;
public:
String(const char* str = nullptr);
String(const String& rhs) :_data(new char[rhs.size() + 1]) {
strcpy(_data, rhs._data);
}
String& operator=(const String& rhs);
~String() {
if (_data != nullptr) {
delete[]_data;
_data = nullptr;
}
//delete[]_data;
}
void Swap(String rhs) {
std::swap(_data, rhs._data);
}
size_t size() const {
return strlen(_data);
}
private:
//重载操作符
char& operator[](int index);
friend ostream& operator<<(ostream &os, const String &rhs);
friend istream& operator >> (istream &is, String &rhs);
friend String operator+(const String& lhs, const String& rhs);
friend String operator+=(String& lhs,const String& rhs); //注意这里不能声明成const
friend bool operator==(const String& lhs, const String& rhs);
friend bool operator!=(const String& lhs, const String& rhs);
friend bool operator>(const String &lhs, const String &rhs);
friend bool operator>=(const String &lhs, const String &rhs);
friend bool operator<(const String &lhs, const String &rhs);
friend bool operator<=(const String &lhs, const String &rhs);
};
String::String(const char* str) { //构造函数,默认的输入为空
if (str == nullptr) {
_data = new char[1];
*_data = '\0';
}
else {
_data = new char[strlen(str) + 1];
strcpy(_data, str);
}
}
//
String& String::operator=(const String& rhs) {
if (this == &rhs) {
return *this;
}
delete[]_data;
_data = new char[rhs.size() + 1];
strcpy(_data, rhs._data);
}
/*
其他的写法:
//2. copy and swap技术
String& operator=(const String& rhs) {
String temp(rhs); //利用复制构造函数
Swap(rhs);
return *this;
}
//3. 传值方式
String& operator=(String rhs) {
Swap(rhs);
return *this;
}
//4. c++11右值引用
String& operator= (String&& rhs) {
Swap(rhs);
return *this;
}
*/
char& String::operator[](int index) {
return _data[index];
}
ostream& operator<<(ostream &os, const String &rhs) {
os << rhs._data;
return os;
}
/*
//注意这里可能会出错,如果有大神这道什么原因,请指教
istream& operator >> (istream &is, String &rhs) {
String tmp;
is >> tmp._data;
rhs = tmp;
return is;
}
*/
//重载+
String operator+(const String& lhs, const String& rhs) {
String res;
int len = lhs.size() + rhs.size();
res._data = new char[len + 1];
strcpy(res._data, lhs._data);
strcat(res._data, rhs._data);
return res;
}
//重载+=
String operator+=(String& lhs, const String& rhs) {
lhs = lhs + rhs; //利用加法即可完成
return lhs;
}
//重载==
bool operator==(const String& lhs, const String& rhs) {
if (strcmp(lhs._data, rhs._data) == 0) return true;
return false;
}
bool operator!=(const String& lhs, const String& rhs) {
if (strcmp(lhs._data, rhs._data) == 0) return false;
return true;
}
//重载>函数
bool operator>(const String &lhs, const String &rhs){
if (strcmp(lhs._data, rhs._data) > 0)
return true;
return false;
}
bool operator<(const String &lhs, const String &rhs) {
if (strcmp(lhs._data, rhs._data) < 0)
return true;
return false;
}
//重载<=函数
bool operator<=(const String &lhs, const String &rhs){
if (strcmp(lhs._data, rhs._data) <= 0)
return true;
return false;
}
//重载>=函数
bool operator>=(const String &lhs, const String &rhs){
if (strcmp(lhs._data, rhs._data) >= 0)
return true;
return false;
}
#endif // !STRING_H