-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.cpp
104 lines (98 loc) · 2.1 KB
/
String.cpp
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
#include <iostream>
#include <string.h>
#include <time.h>
#pragma warning(disable:4996)
using namespace std;
class String;
class String_cnt {
friend class String;
public:
String_cnt(const char* str = "") :m_cnt(0) {
m_data = new char[strlen(str) + 1];
strcpy(m_data, str);
}
String_cnt(const String_cnt& s) {
m_data = s.m_data;
}
String_cnt& operator=(const String_cnt& s) {
if (this != &s) //浅拷贝
m_data = s.m_data;
return *this;
}
~String_cnt() {
delete[]m_data;
m_data = nullptr;
}
void Increase() { ++m_cnt; }
void Decrease() {
--m_cnt;
if (!m_cnt)
delete this;
}
char* GetData() const { return m_data; }
private:
char* m_data;
int m_cnt;
};
class String {
friend ostream& operator<<(ostream& out, const String& s);
public:
explicit String(const char* str = "") :pn(new String_cnt(str)) {
pn->Increase();//构造函数,串计数器+1
}
String(const String& s) :pn(s.pn) { pn->Increase(); }//拷贝构造,浅拷贝,新串计数器+1
String& operator=(const String& s) {
if (this != &s) {
pn->Decrease();//原串计数器-1
pn = s.pn;//浅拷贝指向新串
pn->Increase();//新串计数器+1
}
return *this;
}
~String() { pn->Decrease(); }//析构函数,只需将串的计数器减1
void ToUpper() {
DepthCopy();//写时拷贝
char* str = pn->m_data;//拿到深拷贝的串地址
while (*str) {//小写转大写
if (*str >= 'a' && *str <= 'z')
*str -= 32;
str++;
}
}
void ToLower() {
DepthCopy();//写时拷贝
char* str = pn->m_data;//拿到深拷贝的串地址
while (*str) {//大写转小写
if (*str >= 'A' && *str <= 'Z')
*str += 32;
str++;
}
}
int Size() {
int length = 0;
char* p = pn->m_data;
while (*p)
++length, ++p;
return length;
}
private:
void DepthCopy() {//深拷贝——仅供写实拷贝使用,私有方法
String_cnt* tmp = new String_cnt(pn->m_data);
pn->Decrease();//先将原串的计数器-1
pn = tmp;//获得新串的地址
pn->Increase();//新串计数器+1
}
String_cnt* pn;
};
ostream& operator<<(ostream& out, const String& s) {
out << s.pn->GetData();
return out;
}
int main()
{
String s1("adutsdutfsogfsif");
String s2("31231");
cout << s1.Size() << endl;
cout << s2.Size() << endl;
return 0;
}