Learn R Programming

cppcontainers (version 1.0.4)

cpp_vector: Create vector

Description

Create a vector. Vectors are dynamic, contiguous arrays.

Usage

cpp_vector(x)

Value

Returns a CppVector object referencing a vector in C++.

Arguments

x

An integer, numeric, character, or logical vector.

Details

R vectors are similar to C++ vectors. These sequence containers allow for random access. I.e., you can directly access the fourth element via its index x[4], without iterating through the first three elements before. Vectors are comparatively space-efficient, requiring less RAM per element than many other container types.

One advantage of C++ vectors over R vectors is their ability to reduce the number of copies made during modifications. reserve, e.g., reserves space for future vector extensions.

C++ vector methods implemented in this package are assign, at, back, capacity, clear, emplace, emplace_back, empty, erase, flip, front, insert, max_size, pop_back, push_back, reserve, resize, shrink_to_fit, and size. The package also adds the == and [ operators 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_deque, cpp_forward_list, cpp_list.

Examples

Run this code
v <- cpp_vector(4:6)
v
# 4 5 6

push_back(v, 3L)
v
# 4 5 6 3

print(v, from = 3)
# 6 3

print(v, n = -2)
# 3 6

pop_back(v)
v
# 4 5 6

Run the code above in your browser using DataLab