Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::as_map

mapping_type& as_map();
const mapping_type& as_map() const;

Returns (const) reference to the mapping node value.
If the current node value is not a mapping, a fkyaml::type_error will be thrown.

Return Value

(const) reference to the mapping node value.

Examples

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

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

    // get reference to the sequence value from a non-const node.
    // use `auto&` or `fkyaml::node::sequence_type&` for forward compatibility.
    auto& m = n.as_map();
    // get const reference to the sequence value from a const node.
    const fkyaml::node cn = n;
    const auto& cm = cn.as_map();

    // modify the sequence value.
    m.emplace("baz", 3.14);

    for (auto& pair : m) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }
    std::cout << std::endl;
    for (const auto& cpair : cm) {
        std::cout << cpair.first << ": " << cpair.second << std::endl;
    }
}

output:

bar: 123
baz: 3.14
foo: true

bar: 123
foo: true

See Also