
Last chance! 50% off unlimited learning
Sale ends in
Create a priority queue. Priority queues are hold ordered, non-unique elements.
cpp_priority_queue(x, sorting = c("descending", "ascending"))
Returns a CppPriorityQueue object referencing a priority_queue in C++.
An integer, numeric, character, or logical vector.
"descending"
(default) arranges elements in descending order with the largest element at the top. "ascending"
sorts the
elements in the opposite direction, with the smallest element at the top.
A priority queue is a container, in which the order of the elements depends on their size rather than their time of insertion. As in a stack, elements are removed from the top.
C++ priority queue methods implemented in this package are emplace, empty, pop, push, size, and top. The package also adds various helper functions (print, sorting, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
cpp_queue, cpp_stack.
q <- cpp_priority_queue(4:6)
q
# First element: 6
emplace(q, 10L)
q
# First element: 10
emplace(q, 3L)
q
# First element: 10
top(q)
# [1] 10
q <- cpp_priority_queue(4:6, "ascending")
q
# First element: 4
push(q, 10L)
q
# First element: 4
Run the code above in your browser using DataLab