728x90
728x170
*[header][container] queue : priority_queue
std::priority_queue
template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;
Priority queue
This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).
Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall be accessible through random access iterators and support the following operations:
- empty()
- size()
- front()
- push_back()
- pop_back()
The standard container classes vector and deque fulfill these requirements. By default, if no container class is specified for a particular priority_queue class instantiation, the standard container vector is used.
Support of random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by automatically calling the algorithm functions make_heap, push_heap and pop_heapwhen needed.
Template parameters
- T
- Type of the elements.
Aliased as member type priority_queue::value_type. - Container
- Type of the internal underlying container object where the elements are stored.
Its value_type shall be T.
Aliased as member type priority_queue::container_type. - Compare
- A binary predicate that takes two elements (of type T) as arguments and returns a
bool
.
The expressioncomp(a,b)
, where comp is an object of this type and a and b are elements in the container, shall returntrue
if a is considered to go before b in the strict weak ordering the function defines.
The priority_queue uses this function to maintain the elements sorted in a way that preserves heap properties(i.e., that the element popped is the last according to this strict weak ordering).
This can be a function pointer or a function object, and defaults toless<T>
, which returns the same as applying the less-than operator (a<b
).
Member types
member type | definition | notes |
---|---|---|
value_type | The first template parameter (T) | Type of the elements |
container_type | The second template parameter (Container) | Type of the underlying container |
reference | container_type::reference | usually, value_type& |
const_reference | container_type::const_reference | usually, const value_type& |
size_type | an unsigned integral type | usually, the same as size_t |
Member functions
- (constructor)
- Construct priority queue (public member function )
- empty
- Test whether container is empty (public member function )
- size
- Return size (public member function )
- top
- Access top element (public member function )
- push
- Insert element (public member function )
- emplace
- Construct and insert element (public member function )
- pop
- Remove top element (public member function )
- swap
- Swap contents (public member function )
Non-member function overloads
- swap (queue)
- Exchange contents of priority queues (public member function )
Non-member class specializations
- uses_allocator<queue>
- Uses allocator for priority queue (class template )
내용 출처 : http://www.cplusplus.com/reference/queue/priority_queue/
728x90
그리드형(광고전용)
'Programming > C++' 카테고리의 다른 글
sort 함수 정렬 기준 (0) | 2018.11.17 |
---|---|
랜덤 함수/난수 생성 함수 (Random Function) (0) | 2018.10.07 |
입력된 문자열에서 공백을 제거하여 출력하기 (0) | 2018.09.24 |
Pair Vector (0) | 2017.11.26 |
[header][container] queue : queue (0) | 2017.11.17 |
string형 변수 길이 구하기 (0) | 2017.11.15 |
vector 안의 원소들의 순서를 역순으로 바꾸는 방법 (0) | 2017.11.12 |
정렬 알고리즘의 시간 복잡도 비교 (0) | 2017.11.08 |