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.

  1. Removes the element at pos.
  2. Removes the elements in the range [first; last).
  3. Removes the element at index idx (only works for arrays).
  4. 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 remove
  • first, last: range of elements to remove
  • idx: index of the element to remove
  • key: key of the element to remove

Return value

  1. Iterator following the last removed element. If the iterator pos refers to the last element, the end() iterator is returned.
  2. Iterator following the last removed element. If the iterator pos refers to the last element, the end() iterator is returned.
  3. None.
  4. Number of elements removed.

Complexity

  1. Linear in distance between pos and the end of the container in case of arrays; amortized constant in case of objects; constant otherwise.
  2. Linear in the distance between first and last, plus linear in the distance between last and end of the container in case of arrays; log(c.size()) + std::distance(first, last) where c is the instance of ObjectType in case of objects; constant otherwise.
  3. Linear in distance between idx and the end of the container.
  4. log(c.size()) + c.count(key) where c is the instance of ObjectType.

Exceptions

  1. std::runtime_error when called on null type.
  2. std::runtime_error when called on null type.
  3. std::runtime_error when called on non-array types. std::out_of_range when idx >= size().
  4. std::runtime_error when called on non-object types. Any exceptions thrown by the ObjectType::Compare object.

See also

  • clear clears the contents