Created empty() (markdown)

Niels committed 2015-04-09 18:11:25 +02:00
1 parent 2c8d2b241f
commit fab8c05ec5
1 file changed
+56
+56
@@ -0,0 +1,56 @@
# nlohmann::basicjson::empty
```cpp
bool empty() const;
```
## Description
Checks if a JSON value has no elements, i.e. whether `begin() == end()`. The return value depends on the different value types and is defined as follows:
| Value type | return value |
| ---------- | --------------------- |
| null | `true` |
| number | `false` |
| boolean | `false` |
| string | `false` |
| array | `ArrayType::empty()` |
| object | `ObjectType::empty()` |
## Parameter
None.
## Complexity
Constant, as long as `ArrayType` and `ObjectType` satisfy the [Container concept](http://en.cppreference.com/w/cpp/concept/Container).
## Exceptions
None. The function's noexcept-specification is `noexcept`.
## Example
The following code uses empty to check if a `nlohmann::json` contains any elements:
```cpp
#include <iostream>
#include <json.hpp>
int main()
{
nlohmann::json numbers;
std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';
numbers.push_back(42);
numbers.push_back(13317);
std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}
```
Output:
```
Initially, numbers.empty(): 1
After adding elements, numbers.empty(): 0
```