C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)

2021. 3. 16. 00:25·C++/Overloading

비교 연산자 오버로딩 (Comparison Operator Overloading)

  • if문, std::sort 등을 사용하려면 필수적으로 구현해야 한다.

예제

  • ==, != 오버로딩 예제

    #include <iostream>
    
    class Cents
    {
      int    cents_;
    
    public:
      Cents(int cents = 0) { cents_ = cents; }
    
      bool operator == (Cents& c)
      {
        return (cents_ == c.cents_);
      }
    
      bool operator != (Cents& c)
      {
        return (cents_ != c.cents_);
      }
    
      friend std::ostream& operator << (std::ostream& out, const Cents& cents)
      {
        out << cents.cents_;
        return (out);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Cents    c1{ 6 };
      Cents    c2{ 6 };
      Cents    c3{ 0 };
    
      cout << std::boolalpha;
      cout << (c1 == c2) << endl;
      cout << (c1 != c2) << endl;
      cout << (c1 == c3) << endl;
      cout << (c1 != c3) << endl;
    }
    
    /* stdout
    true
    false
    false
    true
    */
  • < 오버로딩 예제

    • std::sort 함수를 사용하려면 < 연산자를 오버로딩해야 한다.
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    class Cents
    {
      int    cents_;
    
    public:
      Cents(int cents = 0) { cents_ = cents; }
      int& getCents() { return cents_; }
    
      bool    operator < (const Cents& c)
      {
        return (cents_ < c.cents_);
      }
    
      friend std::ostream& operator << (std::ostream& out, const Cents& cents)
      {
        out << cents.cents_;
        return (out);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      vector<Cents>    arr(20);
    
      for (size_t i = 0; i < 20; ++i)
        arr[i].getCents() = i;
    
      std::random_shuffle(begin(arr), end(arr));
    
      for (auto& e : arr)
        cout << e << ' ';
      cout << endl;
    
      std::sort(begin(arr), end(arr));
    
      for (auto& e : arr)
        cout << e << ' ';
      cout << endl;
    }
    
    /* stdout
    12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    */
저작자표시 (새창열림)

'C++ > Overloading' 카테고리의 다른 글

C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)  (0) 2021.03.16
C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)  (0) 2021.03.16
C++ 단항 연산자 오버로딩 (Unary Operator Overloading)  (0) 2021.03.16
C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)  (0) 2021.03.16
C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)  (0) 2021.03.16
'C++/Overloading' 카테고리의 다른 글
  • C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)
  • C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)
  • C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
  • C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    dism
    스프링
    알림
    java
    제외
    EXCLUDE
    시스템 복구
    윈도우 명령어
    Windows 11
    windows
    MacOS
    KakaoTalk
    citrix workspace
    백기선
    spring
    vscode
    SFC
    logi options
    unix
    그림판
    mspaint
    로지텍 마우스 제스처
    Solaris 10
    SunOS 5.1
    스프링 프레임워크 핵심 기술
    Workspace
    윈도우
    맥북 카카오톡 알림 안뜸
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)
상단으로

티스토리툴바