Unlimited learning, half price | 50% off

Last chance! 50% off unlimited learning

Sale ends in


cppcontainers (version 1.0.4)

cpp_priority_queue: Create priority queue

Description

Create a priority queue. Priority queues are hold ordered, non-unique elements.

Usage

cpp_priority_queue(x, sorting = c("descending", "ascending"))

Value

Returns a CppPriorityQueue object referencing a priority_queue in C++.

Arguments

x

An integer, numeric, character, or logical vector.

sorting

"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.

Details

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.

See Also

cpp_queue, cpp_stack.

Examples

Run this code
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