별의 공부 블로그 🧑🏻‍💻
728x90
728x170

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
#include <iostream>
#include <cstring>
#include <compare>  // <=>
using namespace std;
 
class String {
    char* _chars;
public:
    String(const char* chars) : _chars(new char[strlen(chars) + 1]) {
        strcpy(_chars, chars);
    }
 
    bool operator==(const String& s) const {
        return strcmp(_chars, s._chars) == 0;
    }
 
    // C 스타일의 비교 연산
    bool operator==(const char* s) const {
        return strcmp(_chars, s) == 0;
    }
 
    bool operator!=(const String& s) const {
        return strcmp(_chars, s._chars) != 0;       // 방법 1
        // return !(*this == s);                    // 방법 2
    }
 
    bool operator<(const String& s) const {
        return strcmp(_chars, s._chars) < 0;
    }
 
    bool operator>(const String& s) const {
        return strcmp(_chars, s._chars) > 0;
    }
 
    bool operator<=(const String& s) const {
        return strcmp(_chars, s._chars) <= 0;        // 방법 1
        // return !(*this > s);                      // 방법 2
    }
 
    bool operator>=(const String& s) const {
        return strcmp(_chars, s._chars) >= 0;        // 방법 1
        // return !(*this < s);                      // 방법 2
    }
 
    // C++20 부터 사용 가능
    strong_ordering operator<=>(const String& s) const {
        int result = strcmp(_chars, s._chars);
        if (result < 0) {
            return strong_ordering::less;
        }
        if (result > 0) {
            return strong_ordering::greater;
        }
        return strong_ordering::equal;
    }
 
    void print() {
        cout << _chars << endl;
    }
 
    // friend 선언
    friend bool operator==(const char* s0, const String& s1) {
        return strcmp(s0, s1._chars) == 0;
    }
};
 
int main() {
    String s0("abc");
    String s1("abc");
    String s2("abd");
 
    if (s0 == s1) {
        cout << "Equal" << endl;
    }
    else {
        cout << "Not Equal" << endl;
    }
    if (s0 != s2) {
        cout << "Not Equal" << endl;
    }
 
    String s3("c");
    String s4("b");
 
    if (s3 < s4) {
        cout << "less" << endl;
    }
    else {
        cout << "greater" << endl;
    }
 
    String s5("b");
    String s6("a");
 
    if ((s5 <=> s6) == 0) {
        cout << "equal" << endl;
    }
    else if ((s5 <=> s6) > 0) {
        cout << "greater" << endl;
    }
    else {
        cout << "less" << endl;
    }
 
    if (s6 == "a") {
        cout << "equal" << endl;
    }
 
    if ("b" == s5) {
        cout << "equal" << endl;
    }
 
    return 0;
}
 


Equal
Not Equal
greater
greater
equal
equal


728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖