더북(TheBook)

3.4.1 우선순위 큐를 생성하기

빈 우선순위 큐는 다음과 같이 생성할 수 있다.

std::priority_queue<std::string> words;

타입이 일치하는 객체를 범위로 지정해서 우선순위 큐를 초기화할 수도 있다.

std::string wrds[] {"one", "two", "three", "four"};
std::priority_queue<std::string> words {std::begin(wrds),
                                            std::end(wrds)}; // "two" "three" "one "four"

범위는 어떤 소스에서 가져온 순차열이라도 쓸 수 있고, 순차열의 원소들이 정렬된 상태일 필요는 없다. 범위로 지정된 원소들은 우선순위 큐에서 적절하게 정렬된다.

복제 생성자로 타입이 같은, 특히 템플릿 타입 인수가 같은 priority_queue 객체를 복제해서 생성할 수도 있다.

std::priority_queue<std::string> copy_words {words}; // words의 복제본

우측값 참조 매개변수를 이용한 복제 생성자도 이용할 수 있다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.