This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.cpp
89 lines (80 loc) · 1.58 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
#include <iostream>
#include <cstring>
#include "string.h"
#include "memtrace.h"
String::String(const char betu)
{
len = 1;
pData = new char[len + 1];
pData[0] = betu;
pData[1] = '\0';
}
String::String(const char* string)
{
len = 0;
while (string[len] != '\0')
{
len++;
}
pData = new char[len + 1];
for (size_t i = 0; i < len; i++)
{
pData[i] = string[i];
}
pData[len] = '\0';
}
String::String(const String& original)
{
this->len = original.len;
this->pData = new char[this->len + 1];
for (size_t i = 0; i < this->len; i++)
{
this->pData[i] = original[i];
}
this->pData[this->len] = '\0';
}
String& String::operator=(const String& rhs)
{
if (&rhs != this)
{
delete[] this->pData;
this->len = rhs.len;
this->pData = new char[this->len + 1];
for (size_t i = 0; i < this->len; i++)
{
this->pData[i] = rhs[i];
}
this->pData[this->len] = '\0';
}
return *this;
}
const char& String::operator[](unsigned int index) const
{
if (index < 0 || index >= len)
{
throw "rossz index";
}
else
{
return pData[index];
}
}
bool String::operator==(const String &rhs)
{
if (this->size() != rhs.size())
{
return false;
}
for (size_t i = 0; i < this->size(); ++i)
{
if (this->c_str()[i] != rhs.c_str()[i])
{
return false;
}
}
return true;
}
std::ostream& operator<<(std::ostream& os, const String& s0)
{
return os << s0.c_str();
}