-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
117 lines (110 loc) · 3.01 KB
/
String.cpp
File metadata and controls
117 lines (110 loc) · 3.01 KB
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
#include<bits/stdc++.h>
using namespace std;
class String {
private:
char *str;
int size = 0;
public:
// 0 argument constructor
String() {
str = new char[1];
size = 0;
str[0] = '\0';
}
// 1 argument constructor
String(const char *param) {
size = strlen(param);
str = new char[size + 1];
memcpy(str, param, size);
str[size] = '\0';
}
// copy constructor
String(const String& o) {
size = strlen(o.str);
str = new char[size + 1];
memcpy(str, o.str, size);
str[size] = '\0';
}
// copy assignment
String& operator=(const String& o) {
String copy(o.str);
size = o.size;
swap(this->str, copy.str);
return *this;
}
// move constructor
String(String&& o) {
size = strlen(o.str);
swap(this->str, o.str);
}
// move assignment
String& operator=(String&& o) {
size = strlen(o.str);
swap(this->str, o.str);
return *this;
}
// destructor
~String() {
delete[] str;
}
// push_back
void push_back(char c) {
size++;
char* newBuffer = new char[size + 1];
for (int i = 0; i < size - 1; i++) {
newBuffer[i] = str[i];
}
newBuffer[size - 1] = c;
newBuffer[size] = '\0';
delete[] str;
str = newBuffer;
}
// access string element
char& operator[](int pos) {
if (pos < 0 || pos >= size) {
throw out_of_range("Index out of bound: " + to_string(pos));
}
return str[pos];
}
// + opeartor
String operator+(const String& o) {
int newSize = size + o.size;
char *newBuffer = new char[newSize + 1];
memcpy(newBuffer, str, size);
memcpy(newBuffer + size, o.str, o.size);
newBuffer[newSize] = '\0';
String result(newBuffer);
return result;
}
// += operator
String& operator+=(const String& o) {
int newSize = size + o.size;
char *newBuffer = new char[newSize + 1];
memcpy(newBuffer, str, size);
memcpy(newBuffer + size, o.str, o.size);
newBuffer[newSize] = '\0';
delete[] str;
str = newBuffer;
size = newSize;
return *this;
}
// << operator
friend ostream& operator<<(ostream& os, const String& s) {
// Inferior implementation since we're making iostream calculate the
// size when we already know it:
// os << s.str;
// return os;
// better way:
return os.write(s.str, static_cast<streamsize>(s.size));
}
};
int main() {
String s("Hello world! ");
String t("I'm a noob and try to print");
s += t;
s[6] = 'W';
s = s + ". Now I check the + operator";
cout << s << endl;
s = s + ". Okay seem good!";
cout << s << endl;
}