Learn R Programming

cppcontainers (version 1.0.4)

try_emplace: Add an element

Description

Add an element to a container by reference in place, if it does not exist yet.

Usage

try_emplace(x, value, key)

Value

Invisibly returns NULL.

Arguments

x

A CppMap or CppUnorderedMap object.

value

A value to add to x.

key

A key to add to x.

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.

See Also

emplace, emplace_after, emplace_back, emplace_front, insert, insert_or_assign.

Examples

Run this code
m <- cpp_map(4:6, 9:11)
m
# [4,9] [5,10] [6,11]

try_emplace(m, 13L, 8L)
m
# [4,9] [5,10] [6,11] [8,13]

try_emplace(m, 12L, 4L)
m
# [4,9] [5,10] [6,11] [8,13]

Run the code above in your browser using DataLab