Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::contains

template <typename KeyType>
bool contains(KeyType&& key) const; // (1)

template <typename BasicNodeType>
bool contains(BasicNodeType&& key) const; // (2)

Checks if the YAML node has the given key in the queried mapping.
If the node value is not a mapping, a fkyaml::type_error will be thrown.

The input parameter key must be either a basic_node obejct or an object of a compatible type, i.e., a type with which a basic_node object can be constructible.
Note that the overload (1) internally constructs a temporal basic_node object.
So, if you use the same key multiple times, for example, in a for loop, consider creating a basic_node as a key first for better performance.

Template Parameters

KeyType
A type with which a basic_node object can be constructible.
BasicNodeType
A basic_node template instance type.

Parameters

key [in]
A key to check existence in the queried mapping.

Return Value

true if the key exists in the queried mapping, false otherwise.

Example
#include <iomanip>
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a YAML mapping node.
    fkyaml::node n = {{"foo", true}, {"bar", 123}};

    // check if the node has the following keys.
    std::cout << std::boolalpha;
    std::cout << n.contains("foo") << std::endl;
    std::cout << n.contains(fkyaml::node("baz")) << std::endl;

    // create a YAML node. (not mapping)
    fkyaml::node n2 = "qux";

    // check if the node has the following key.
    std::cout << std::boolalpha << n2.contains("qux") << std::endl;

    return 0;
}

output:

true
false
false

See Also