개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.04.11 23:48

SplPriorityQueue compare 메서드 이해 필요

목록
  • Haskell연구가 1일 전 2025.04.11 23:48
  • 19
    1
스프링 프레임워크에서 PriorityQueue를 사용할 때, compare 메서드를 오버라이딩하는 방법을 알고 싶습니다.

PriorityQueue의 compare 메서드는 두 개의 객체를 비교하는 메서드입니다. 이 메서드는 PriorityQueue가 어떤 객체를 우선순위로 추가할지 결정하는 데 사용됩니다.

compare 메서드를 오버라이딩하는 방법은 다음과 같습니다.

php

class MyObject implements Comparable {

    private $value;



    public function __construct($value) {

        $this->value = $value;

    }



    public function compareTo(MyObject $other) {

        if ($this->value < $other->value) {

            return -1; // this 객체가 우선순위가 높다

        } elseif ($this->value > $other->value) {

            return 1; // other 객체가 우선순위가 높다

        } else {

            return 0; // 값이 같음

        }

    }

}



위 예제에서, MyObject 클래스는 PriorityQueue에서 사용할 객체입니다. compareTo 메서드는 두 개의 MyObject 객체를 비교하며, return 값은 다음과 같이 해석됩니다.

* -1: this 객체가 우선순위가 높다
* 0: 값이 같음
* 1: other 객체가 우선순위가 높다

이러한 compareTo 메서드를 오버라이딩하면, PriorityQueue는 두 개의 객체를 비교할 때 이 메서드를 호출합니다.

위 예제에서, PriorityQueue는 MyObject 객체를 우선순위에 따라 추가합니다.

php

$queue = new SplPriorityQueue();

$queue->insert(new MyObject(5), 5);

$queue->insert(new MyObject(3), 3);

$queue->insert(new MyObject(8), 8);



while (!$queue->valid()) {

    echo $queue->extract()->value . "n";

}



위 예제에서, PriorityQueue는 MyObject 객체를 우선순위에 따라 추가하고, 가장 높은 우선순위의 객체를 먼저 추출합니다.

이러한 compareTo 메서드를 오버라이딩하면, PriorityQueue는 객체를 우선순위에 따라 추가하고, 가장 높은 우선순위를 가진 객체

    댓글목록

    profile_image
    나우호스팅  1일 전



    PriorityQueue의 compare 메서드를 오버라이딩하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    class MyObject implements Comparable {
    
        private $value;
    
    
    
        public function __construct($value) {
    
            $this->value = $value;
    
        }
    
    
    
        public function compareTo(MyObject $other) {
    
            if ($this->value < $other->value) {
    
                return -1; // this 객체가 우선순위가 높다
    
            } elseif ($this->value > $other->value) {
    
                return 1; // other 객체가 우선순위가 높다
    
            } else {
    
                return 0; // 값이 같음
    
            }
    
        }
    
    }
    
    


    위 예제에서, MyObject 클래스는 PriorityQueue에서 사용할 객체입니다. compareTo 메서드는 두 개의 MyObject 객체를 비교하고, return 값은 다음과 같이 해석됩니다.

    * -1: this 객체가 우선순위가 높다
    * 0: 값이 같음
    * 1: other 객체가 우선순위가 높다

    이러한 compareTo 메서드를 오버라이딩하면, PriorityQueue는 두 개의 객체를 비교할 때 이 메서드를 호출합니다.

    #hostingforum.kr
    php
    
    $queue = new SplPriorityQueue();
    
    $queue->insert(new MyObject(5, 5);
    
    $queue->insert(new MyObject(3, 3);
    
    $queue->insert(new MyObject(8, 8);
    
    
    
    while ($queue->valid()) {
    
        echo $queue->extract()->value . "n";
    
    }
    
    


    위 예제에서, PriorityQueue는 MyObject 객체를 우선순위에 따라 추가하고, 가장 높은 우선순위를 가진 객체를 먼저 추출합니다.

    이러한 compareTo 메서드를 오버라이르는 경우, PriorityQueue는 객체를 우선순위에 따라 추가하고, 가장 높은 우선순위를 가진 객체를 먼저 추출합니다.

    2025-04-11 23:49

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 11,124건 / 21 페이지

검색

게시물 검색