File: try_emplace.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 (57 lines) | stat: -rw-r--r-- 1,254 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
### jsoncons::basic_json::try_emplace

```cpp
template <typename T>
pair<object_iterator, bool> try_emplace(const string_view_type& key, 
                                        Args&&... args); (1)

template <typename T>
object_iterator try_emplace(const_object_iterator hint, 
                            const string_view_type& key, 
                            Args&&... args); (2)
```

#### Parameters

    key
The key used both to look up and to insert if not found

    hint
Iterator to the position before which the new element will be inserted

    args        
Arguments to forward to the constructor of the element

#### Return value

(1) returns a pair consisting of first, an iterator to the inserted value 
or the already existing value, 
and second, a bool indicating whether the insertion took place
(true for insertion, false for no insertion.)

(2) returns an iterator to the inserted value 
or the already existing value. 

#### Exceptions

Throws `std::domain_error` if not a json object.

### Example

```cpp
json a;

a.try_emplace("object1",json());
a.try_emplace("field1","value1");
a["object1"].try_emplace("field2","value2");

std::cout << a << '\n';
```
Output:

```json
{"field1":"value1","object1":{"field2":"value2"}}
```