
SplPriorityQueue::extract 메서드는 SplPriorityQueue 객체에서 우선순위가 가장 높은 요소를 제거하고 반환하는 메서드입니다. 만약 SplPriorityQueue 객체에 요소가 하나도 없을 때 extract 메서드를 호출하면 Exception을 발생시킵니다.
예를 들어, 다음과 같은 코드를 실행하면 Exception을 발생시킵니다.
#hostingforum.kr
php
$splQueue = new SplPriorityQueue();
try {
$splQueue->extract();
} catch (Exception $e) {
echo $e->getMessage(); // "SplDoublyLinkedList::extract(): It is not safe to use objects after they go out of scope"
}
만약 요소를 제거한 후 반환된 요소를 사용해야 하는 경우, 다음과 같이 코드를 작성할 수 있습니다.
#hostingforum.kr
php
$splQueue = new SplPriorityQueue();
$splQueue->insert('A', 1);
$splQueue->insert('B', 2);
$extractedElement = $splQueue->extract();
echo $extractedElement; // 'B'가 출력됩니다.
위의 코드를 실행하면 'B'가 출력됩니다.
2025-05-20 14:11