mirror of
https://github.com/nlohmann/json.git
synced 2026-07-09 12:05:10 +00:00
deploy: 7c9208bfb3
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
# nlohmann::basic_json::get_allocator
|
||||
|
||||
```
|
||||
static allocator_type get_allocator();
|
||||
```
|
||||
|
||||
Returns the allocator associated with the container.
|
||||
|
||||
## Return value
|
||||
|
||||
associated allocator
|
||||
|
||||
## Examples
|
||||
|
||||
Example
|
||||
|
||||
The example shows how `get_allocator()` is used to created `json` values.
|
||||
|
||||
```
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
auto alloc = json::get_allocator();
|
||||
using traits_t = std::allocator_traits<decltype(alloc)>;
|
||||
|
||||
json* j = traits_t::allocate(alloc, 1);
|
||||
traits_t::construct(alloc, j, "Hello, world!");
|
||||
|
||||
std::cout << *j << std::endl;
|
||||
|
||||
traits_t::destroy(alloc, j);
|
||||
traits_t::deallocate(alloc, j, 1);
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
"Hello, world!"
|
||||
```
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 1.0.0.
|
||||
Reference in New Issue
Block a user