1
0
mirror of https://github.com/nlohmann/json synced 2024-11-27 06:10:06 +00:00
nlohmannjson/docs/empty.md
2015-04-01 23:23:36 +02:00

1.2 KiB

nlohmann::basic_json::empty

bool empty() const noexcept;

Checks if the container has no elements; that is, whether begin() == end().

Parameters

(none)

Return value

true if the container is empty, false otherwise. Note that the JSON types string, number, and boolean are never empty, null values are always empty.

Exceptions

noexcept specification: noexcept.

Complexity

Constant (assuming types ObjectType and ArrayType satisfy the Container concept).

Example

The following code uses empty to check if a json container contains any elements:

#include <json.hpp>
#include <iostream>
  
int main()
{
    nlohman::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

Requirements

The empty member function is part of the Container requirement.

See also

  • size()