2020-08-12 19:41:59 +08:00
|
|
|
# basic_json::update
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
// (1)
|
|
|
|
void update(const_reference j);
|
|
|
|
|
|
|
|
// (2)
|
|
|
|
void update(const_iterator first, const_iterator last);
|
|
|
|
```
|
|
|
|
|
|
|
|
1. Inserts all values from JSON object `j` and overwrites existing keys.
|
|
|
|
2. Inserts all values from from range `[first, last)` and overwrites existing keys.
|
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
The function is motivated by Python's [dict.update](https://docs.python.org/3.6/library/stdtypes.html#dict.update)
|
|
|
|
function.
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
|
|
`j` (in)
|
|
|
|
: JSON object to read values from
|
|
|
|
|
|
|
|
`first` (in)
|
|
|
|
: begin of the range of elements to insert
|
|
|
|
|
|
|
|
`last` (in)
|
|
|
|
: end of the range of elements to insert
|
|
|
|
|
|
|
|
## Exceptions
|
|
|
|
|
2020-08-15 21:18:07 +08:00
|
|
|
1. The function can throw the following exceptions:
|
2020-08-12 20:29:25 +08:00
|
|
|
- Throws [`type_error.312`](../../home/exceptions.md#jsonexceptiontype_error312) if called on JSON values other than
|
|
|
|
objects; example: `"cannot use update() with string"`
|
2020-08-12 19:41:59 +08:00
|
|
|
2. The function can throw thw following exceptions:
|
2020-08-12 20:29:25 +08:00
|
|
|
- Throws [`type_error.312`](../../home/exceptions.md#jsonexceptiontype_error312) if called on JSON values other than
|
|
|
|
objects; example: `"cannot use update() with string"`
|
|
|
|
- Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an
|
|
|
|
iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"`
|
|
|
|
- Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last`
|
|
|
|
do not belong to the same JSON value; example: `"iterators do not fit"`
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
## Complexity
|
|
|
|
|
|
|
|
1. O(N*log(size() + N)), where N is the number of elements to insert.
|
|
|
|
2. O(N*log(size() + N)), where N is the number of elements to insert.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
??? example
|
|
|
|
|
|
|
|
The example shows how `update()` is used.
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
--8<-- "examples/update.cpp"
|
|
|
|
```
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
```json
|
|
|
|
--8<-- "examples/update.output"
|
|
|
|
```
|
|
|
|
|
|
|
|
??? example
|
|
|
|
|
|
|
|
The example shows how `update()` is used.
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
--8<-- "examples/update__range.cpp"
|
|
|
|
```
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
```json
|
|
|
|
--8<-- "examples/update__range.output"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Version history
|
|
|
|
|
|
|
|
- Added in version 3.0.0.
|