Learn R Programming

cppcontainers (version 1.0.4)

cpp_stack: Create stack

Description

Create a stack. Stacks are last-in, first-out containers.

Usage

cpp_stack(x)

Value

Returns a CppStack object referencing a stack in C++.

Arguments

x

An integer, numeric, character, or logical vector.

Details

The last element added to a stack is the first one to remove.

C++ stack methods implemented in this package are emplace, empty, pop, push, size, and top. The package also adds the == operator and various helper functions (print, 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_priority_queue.

Examples

Run this code
s <- cpp_stack(4:6)
s
# Top element: 6

emplace(s, 3L)
s
# Top element: 3

push(s, 9L)
s
# Top element: 9

pop(s)
s
# Top element: 3

Run the code above in your browser using DataLab