mirror of
https://github.com/nlohmann/json.git
synced 2026-10-13 17:57:14 +00:00
deploy: 40a62947d3
This commit is contained in:
1 parent
79119509a9
commit
22fba27145
555 files changed
+17342
-1394
No files matched your search
@@ -52,13 +52,50 @@ Logarithmic in the size of the container, O(log(`size()`)).
|
||||
the same key.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/emplace.cpp"
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json object = {{"one", 1}, {"two", 2}};
|
||||
json null;
|
||||
|
||||
// print values
|
||||
std::cout << object << '\n';
|
||||
std::cout << null << '\n';
|
||||
|
||||
// add values
|
||||
auto res1 = object.emplace("three", 3);
|
||||
null.emplace("A", "a");
|
||||
null.emplace("B", "b");
|
||||
|
||||
// the following call will not add an object, because there is already
|
||||
// a value stored at key "B"
|
||||
auto res2 = null.emplace("B", "c");
|
||||
|
||||
// print values
|
||||
std::cout << object << '\n';
|
||||
std::cout << *res1.first << " " << std::boolalpha << res1.second << '\n';
|
||||
|
||||
std::cout << null << '\n';
|
||||
std::cout << *res2.first << " " << std::boolalpha << res2.second << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/emplace.output"
|
||||
{"one":1,"two":2}
|
||||
null
|
||||
{"one":1,"three":3,"two":2}
|
||||
3 true
|
||||
{"A":"a","B":"b"}
|
||||
"b" false
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
Reference in new issue
Block a user