[,CppMap-method {cppcontainers}R Documentation

Access or insert elements without bounds checking

Description

Read or insert a value by key in a CppMap or CppUnorderedMap. Read a value by index in a CppVector or CppDeque.

Usage

## S4 method for signature 'CppMap'
x[i]

## S4 method for signature 'CppUnorderedMap'
x[i]

## S4 method for signature 'CppVector'
x[i]

## S4 method for signature 'CppDeque'
x[i]

Arguments

x

A CppMap, CppUnorderedMap, CppVector, or CppDeque object.

i

A key (CppMap, CppUnorderedMap) or index (CppVector, CppDeque).

Details

In the two associative container types (CppMap, CppUnorderedMap), [] accesses a value by its key. If the key does not exist, it enters the key with a default value into the container. The default value is 0 for integer and double, an empty string for string, and FALSE for boolean.

In the two sequence container types (CppVector, CppDeque), [] accesses a value by its index. If the index is outside the container, this crashes the program.

at and [] both access elements. Unlike [], at checks the bounds of the container and throws an error, if the element does not exist.

Value

Returns the value associated with i.

See Also

at, back, contains, front, top.

Examples

m <- cpp_map(4:6, seq.int(0, 1, by = 0.5))
m
# [4,0] [5,0.5] [6,1]

m[6L]
# [1] 1

m
# [4,0] [5,0.5] [6,1] 

m[8L]
# [1] 0

m
# [4,0] [5,0.5] [6,1] [8,0]

v <- cpp_vector(4:6)
v
# 4 5 6

v[1L]
# [1] 4

v
# 4 5 6


[Package cppcontainers version 1.0.0 Index]