Created nlohmann::basicjson::operator[] (markdown)

Niels committed 2015-04-12 15:52:24 +02:00
1 parent e050105fc8
commit e79f539583
1 file changed
+34
+34
@@ -0,0 +1,34 @@
```cpp
reference operator[](size_type idx); // 1
const_reference operator[](size_type idx) const; // 2
```
```cpp
reference operator[](const object_t::key_type& key) // 3
const_reference operator[](const object_t::key_type& key) const // 4
```
## Description
1. Returns a reference to the element at specified index `idx`. No bounds checking is performed. In case `idx > size()`, null values are inserted between `size()` and `idx - 1` (only works for arrays).
2. Returns a reference to the element at specified index `idx`. No bounds checking is performed and no values will be inserted (only works for arrays).
3. Returns a reference to the value that is mapped to a key equivalent to `key`, performing an insertion if such key does not already exist. If an insertion is performed, the mapped value is value-initialized (default-constructed for class types, zero-initialized otherwise) and a reference to it is returned. No bounds checking is performed (only works for objects).
4. Returns a reference to the mapped value of the element with key equivalent to `key`. No bounds checking is performed and no values will be inserted (only works for objects).
## Parameters
- `idx`: index of the element to return
- `key`: key of the element to return
## Return value
Reference to the specified value.
## Complexity
1. Constant.
2. Logarithmic in the size of the container.
## Exceptions
1. `std::runtime_error` when called on a JSON container with other type than `array`.
2. `std::runtime_error` when called on a JSON container with other type than `object`.