nlohmann::basic_json::erase - renxiaobo27/json GitHub Wiki
iterator erase(iterator pos);
const_iterator erase(const_iterator pos); // 1
iterator erase(iterator first, iterator last);
const_iterator erase(const_iterator first, const_iterator last); // 2
void erase(const size_type idx); // 3
size_type erase(const typename object_t::key_type& key); // 4
Description
Removes specified elements from the JSON value.
- Removes the element at
pos. - Removes the elements in the range [
first;last). - Removes the element at index
idx(only works for arrays). - Removes the element with key
key(only works for objects).
Invalidates iterators and references at or after the point of the erase, including the end() iterator.
The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos.
The iterator first does not need to be dereferenceable if first==last: erasing an empty range is a no-op.
If erase is called with valid and dereferenceable iterators for JSON values of type number, string, or boolean, the value will be changed to null.
Parameter
pos: iterator to the element to removefirst,last: range of elements to removeidx: index of the element to removekey: key of the element to remove
Return value
- Iterator following the last removed element. If the iterator pos refers to the last element, the
end()iterator is returned. - Iterator following the last removed element. If the iterator pos refers to the last element, the
end()iterator is returned. - None.
- Number of elements removed.
Complexity
- Linear in distance between
posand the end of the container in case of arrays; amortized constant in case of objects; constant otherwise. - Linear in the distance between
firstandlast, plus linear in the distance betweenlastand end of the container in case of arrays;log(c.size()) + std::distance(first, last)wherecis the instance ofObjectTypein case of objects; constant otherwise. - Linear in distance between
idxand the end of the container. log(c.size()) + c.count(key)wherecis the instance ofObjectType.
Exceptions
std::runtime_errorwhen called on null type.std::runtime_errorwhen called on null type.std::runtime_errorwhen called on non-array types.std::out_of_rangewhenidx >= size().std::runtime_errorwhen called on non-object types. Any exceptions thrown by theObjectType::Compareobject.
See also
- clear clears the contents