try_emplace {cppcontainers} | R Documentation |
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)
Arguments
x |
A CppMap or CppUnorderedMap object. |
value |
A value to add to |
key |
A key to add to |
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.
Value
Invisibly returns NULL
.
See Also
emplace, emplace_after, emplace_back, emplace_front, insert, insert_or_assign.
Examples
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]
[Package cppcontainers version 1.0.0 Index]