From fab8c05ec5fb52157bd897a401ab5caa9f32e9b8 Mon Sep 17 00:00:00 2001 From: Niels Date: Thu, 9 Apr 2015 18:11:25 +0200 Subject: [PATCH] Created empty() (markdown) --- empty().md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 empty().md diff --git a/empty().md b/empty().md new file mode 100644 index 0000000..2877f3b --- /dev/null +++ b/empty().md @@ -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 +#include + +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 +``` \ No newline at end of file