Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::begin, fkyaml::basic_node::cbegin¶
Returns a (constant) range of mapping entries.
If a basic_node is not a mapping, a fkyaml::type_error
will be thrown.
This function allows accessing the key()
and value()
functions provided by iterator
and const_iterator
in range-based for loops:
// without map_items()
for (auto it : mapping) {
// `it` is of type fkyaml::node::reference and has no key() member function.
std::cout << "value: " << it << std::endl;
}
// with map_items()
for (auto it : mapping.map_items()) {
// `it` is now of type fkyaml::node::iterator and has key() member function.
std::cout << "key: " << it.key() << ", value: " << it.value() << std::endl;
}
Also, this function allows using structured bindings (since C++17):
for (auto& [key, value] : mapping.map_items()) {
// `key` and `value` are both of type fkyaml::node::reference.
std::cout << "key: " << key << ", value: " << value << std::endl;
}
Return Value¶
A (constant) iterator to the first element of a container node.
Examples¶
Example
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
// create a mapping node.
fkyaml::node map_node = {{"foo", 123}, {"bar", true}};
// print all the mapping entries.
for (auto it : map_node.map_items()) {
std::cout << "key: " << it.key() << ", value: " << it.value() << std::endl;
}
fkyaml::node seq_node = {123, false, 3.14};
try {
// map_items() cannot be called on a sequence node.
auto invalid = seq_node.map_items();
}
catch (const fkyaml::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
output: