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

2021. 3. 16. 00:25·SW개발/C++
반응형

비교 연산자 오버로딩 (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
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

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
'SW개발/C++' 카테고리의 다른 글
  • C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)
  • C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)
  • C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
  • C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바