• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::back
2
3```cpp
4reference back();
5
6const_reference back() const;
7```
8
9Returns a reference to the last element in the container. For a JSON container `c`, the expression `c.back()` is
10equivalent to
11
12```cpp
13auto tmp = c.end();
14--tmp;
15return *tmp;
16```
17
18## Return value
19
20In case of a structured type (array or object), a reference to the last element is returned. In case of number, string,
21boolean, or binary values, a reference to the value is returned.
22
23## Exceptions
24
25If the JSON value is `#!json null`, exception
26[`invalid_iterator.214`](../../home/exceptions.md#jsonexceptioninvalid_iterator214) is thrown.
27
28## Exception safety
29
30Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
31
32## Complexity
33
34Constant.
35
36## Note
37
38!!! danger
39
40    Calling `back` on an empty array or object is undefined behavior and is **guarded by an assertion**!
41
42## Example
43
44??? example
45
46    The following code shows an example for `back()`.
47
48    ```cpp
49    --8<-- "examples/back.cpp"
50    ```
51
52    Output:
53
54    ```json
55    --8<-- "examples/back.output"
56    ```
57
58## Version history
59
60- Added in version 1.0.0.
61- Adjusted code to return reference to binary values in version 3.8.0.
62