File: erase.md

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (105 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
### `jsoncons::basic_json::erase`

```cpp
void erase(const_array_iterator pos);           (1)   (until 0.168.6)
array_iterator erase(const_array_iterator pos); (1)   (since 0.168.6)

void erase(const_array_iterator first, const_array_iterator last);           (2)   (until 0.168.6)
array_iterator erase(const_array_iterator first, const_array_iterator last); (2)   (since 0.168.6)

void erase(const_object_iterator pos);            (3)   (until 0.168.6)
object_iterator erase(const_object_iterator pos); (3)   (since 0.168.6)

void erase(const_object_iterator first, const_object_iterator last);            (4)   (until 0.168.6)
object_iterator erase(const_object_iterator first, const_object_iterator last); (4)   (since 0.168.6)

void erase(const string_view_type& name); (5)
```

(1) Remove an element from an array at the specified position.
Throws `std::domain_error` if not an array.

(2) Remove the elements from an array in the range '[first,last)'.
Throws `std::domain_error` if not an array.

(3) Remove a member from an object at the specified position.
Throws `std::domain_error` if not an object.
    
(4) Remove the members from an object in the range '[first,last)'.
Throws `std::domain_error` if not an object.

(5) Remove a member with the specified name from an object
Throws `std::domain_error` if not an object.

### Examples

#### Iterating an array and erasing elements (since 0.168.6)

```cpp
#include <jsoncons/json.hpp>

using jsoncons::json;

int main()
{
    std::string input = R"(
        ["a","b","c","d","e","f"]
)";

    json j = json::parse(input);
    auto it = j.array_range().begin();
    while (it != j.array_range().end())
    {
        if (*it == "a" || *it == "c")
        {
            it = j.erase(it);
        }
        else
        {
            it++;
        }
    }

    std::cout << j << "\n\n";
}
```
Output:
```json
["b","d","e","f"]
```

#### Iterating an object and erasing members (since 0.168.6)

```cpp
#include <jsoncons/json.hpp>

using jsoncons::json;

int main()
{
    std::string input = R"(
        {"a":1, "b":2, "c":3, "d":4}
)";

    json j = json::parse(input);
    auto it = j.object_range().begin();
    while (it != j.object_range().end())
    {
        if (it->key() == "a" || it->key() == "c")
        {
            it = j.erase(it);
        }
        else
        {
            it++;
        }
    }

    std::cout << j << "\n\n";
}
```
Output:
```json
{"b":2,"d":4}
```