Learn R Programming

cppcontainers (version 1.0.4)

emplace: Add an element

Description

Add an element to a container by reference in place.

Usage

emplace(x, value, key = NULL, position = NULL)

Value

Invisibly returns NULL.

Arguments

x

A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppStack, CppQueue, CppPriorityQueue, CppVector, CppDeque, or CppList object.

value

A value to add to x.

key

A key to add to x. Only relevant for CppMap, CppUnorderedMap, CppMultimap, and CppUnorderedMultimap objects.

position

The index at which to add the element. Only relevant for CppVector, CppDeque, and CppList objects. Indices start at 1.

Details

Existing container values are not overwritten. I.e. inserting a key-value pair into a map that already contains that key preserves the old value and discards the new one. Use insert_or_assign to overwrite values.

emplace and try_emplace produce the same results in the context of this package. try_emplace can be minimally more computationally efficient than emplace.

The emplace methods only add single elements. Use insert to add multiple elements in one call.

See Also

assign, emplace_after, emplace_back, emplace_front, insert, try_emplace.

Examples

Run this code
s <- cpp_set(c(1.5, 2.3, 4.1))
s
# 1.5 2.3 4.1

emplace(s, 3.1)
s
# 1.5 2.3 3.1 4.1

m <- cpp_unordered_map(c("hello", "there"), c(TRUE, FALSE))
m
# ["there",FALSE] ["hello",TRUE]

emplace(m, TRUE, "world")
m
# ["world",TRUE] ["there",FALSE] ["hello",TRUE]

d <- cpp_deque(4:6)
d
# 4 5 6

emplace(d, 9, position = 2)
d
# 4 9 5 6

Run the code above in your browser using DataLab