JSON for Modern C++  3.7.0

◆ erase() [3/4]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
size_type nlohmann::basic_json::erase ( const typename object_t::key_type &  key)
inline

Removes elements from a JSON object with the key value key.

Parameters
[in]keyvalue of the elements to remove
Returns
Number of elements removed. If ObjectType is the default std::map type, the return value will always be 0 (key was not found) or 1 (key was found).
Postcondition
References and iterators to the erased elements are invalidated. Other references and iterators are not affected.
Exceptions
type_error.307when called on a type other than JSON object; example: "cannot use erase() with null"
Complexity
log(size()) + count(key)
Example
The example shows the effect of erase().
1 #include <iostream>
2 #include <nlohmann/json.hpp>
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  // create a JSON object
9  json j_object = {{"one", 1}, {"two", 2}};
10 
11  // call erase
12  auto count_one = j_object.erase("one");
13  auto count_three = j_object.erase("three");
14 
15  // print values
16  std::cout << j_object << '\n';
17  std::cout << count_one << " " << count_three << '\n';
18 }
basic_json<> json
default JSON class
Definition: json.hpp:2355

Output (play with this example online):
{"two":2}
1 0
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/erase__key_type.cpp -o erase__key_type 
See also
erase(IteratorType) – removes the element at a given position
erase(IteratorType, IteratorType) – removes the elements in the given range
erase(const size_type) – removes the element from an array at the given index
Since
version 1.0.0

Definition at line 18258 of file json.hpp.