1 # <small>nlohmann::basic_json::</small>erase
5 iterator erase(iterator pos);
6 const_iterator erase(const_iterator pos);
9 iterator erase(iterator first, iterator last);
10 const_iterator erase(const_iterator first, const_iterator last);
13 size_type erase(const typename object_t::key_type& key);
16 void erase(const size_type idx);
19 1. Removes an element from a JSON value specified by iterator `pos`. The iterator `pos` must be valid and
20 dereferenceable. Thus, the `end()` iterator (which is valid, but is not dereferenceable) cannot be used as a value for
23 If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`.
25 2. Remove an element range specified by `[first; last)` from a JSON value. The iterator `first` does not need to be
26 dereferenceable if `first == last`: erasing an empty range is a no-op.
28 If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`.
30 3. Removes an element from a JSON object by key.
32 4. Removes an element from a JSON array by index.
37 : iterator to the element to remove
40 : iterator to the beginning of the range to remove
43 : iterator past the end of the range to remove
46 : object key of the elements to remove
49 : array index of the element to remove
53 1. Iterator following the last removed element. If the iterator `pos` refers to the last element, the `end()` iterator
55 2. Iterator following the last removed element. If the iterator `last` refers to the last element, the `end()` iterator
57 3. Number of elements removed. If `ObjectType` is the default `std::map` type, the return value will always be `0`
58 (`key` was not found) or `1` (`key` was found).
63 Strong exception safety: if an exception occurs, the original value stays intact.
67 1. The function can throw the following exceptions:
68 - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) if called on a `null` value;
69 example: `"cannot use erase() with null"`
70 - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an
71 iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"`
72 - Throws [`invalid_iterator.205`](../../home/exceptions.md#jsonexceptioninvalid_iterator205) if called on a
73 primitive type with invalid iterator (i.e., any iterator which is not `begin()`); example: `"iterator out of
75 2. The function can throw the following exceptions:
76 - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) if called on a `null` value;
77 example: `"cannot use erase() with null"`
78 - Throws [`invalid_iterator.203`](../../home/exceptions.md#jsonexceptioninvalid_iterator203) if called on iterators
79 which does not belong to the current JSON value; example: `"iterators do not fit current value"`
80 - Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if called on a
81 primitive type with invalid iterators (i.e., if `first != begin()` and `last != end()`); example: `"iterators out
83 3. The function can throw the following exceptions:
84 - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than
85 JSON object; example: `"cannot use erase() with null"`
86 4. The function can throw the following exceptions:
87 - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than
88 JSON object; example: `"cannot use erase() with null"`
89 - Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) when `idx >= size()`; example:
90 `"array index 17 is out of range"`
94 1. The complexity depends on the type:
95 - objects: amortized constant
96 - arrays: linear in distance between `pos` and the end of the container
97 - strings and binary: linear in the length of the member
98 - other types: constant
99 2. The complexity depends on the type:
100 - objects: `log(size()) + std::distance(first, last)`
101 - arrays: linear in the distance between `first` and `last`, plus linear
102 in the distance between `last` and end of the container
103 - strings and binary: linear in the length of the member
104 - other types: constant
105 3. `log(size()) + count(key)`
106 4. Linear in distance between `idx` and the end of the container.
110 1. Invalidates iterators and references at or after the point of the `erase`, including the `end()` iterator.
112 3. References and iterators to the erased elements are invalidated. Other references and iterators are not affected.
117 ??? example "Example: (1) remove element given an iterator"
119 The example shows the effect of `erase()` for different JSON types using an iterator.
122 --8<-- "examples/erase__IteratorType.cpp"
128 --8<-- "examples/erase__IteratorType.output"
131 ??? example "Example: (2) remove elements given an iterator range"
133 The example shows the effect of `erase()` for different JSON types using an iterator range.
136 --8<-- "examples/erase__IteratorType_IteratorType.cpp"
142 --8<-- "examples/erase__IteratorType_IteratorType.output"
145 ??? example "Example: (3) remove element from a JSON object given a key"
147 The example shows the effect of `erase()` for different JSON types using an object key.
150 --8<-- "examples/erase__key_type.cpp"
156 --8<-- "examples/erase__key_type.output"
159 ??? example "Example: (4) remove element from a JSON array given an index"
161 The example shows the effect of `erase()` using an array index.
164 --8<-- "examples/erase__size_type.cpp"
170 --8<-- "examples/erase__size_type.output"
175 - Added in version 1.0.0.
176 - Added support for binary types in version 3.8.0.