Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::iterator, fkyaml::basic_node::const_iterator

using iterator = detail::iterator<basic_node>;
using iterator = detail::iterator<const basic_node>;

The types of non-const/const iterators for basic_node containers.
They satisfies LegacyBidirectionalIterator and are commonly used for sequence and mapping container nodes.

Member Types

  • iterator
    | Type | Definition | | ------------------- | --------------------------------------------------------------------------------------------- | | value_type | basic_node | | reference | basic_node& | | pointer | basic_node* | | difference_type | std::ptrdiff_t | | iterator_category | std::bidirectional_iterator_tag |
  • const_iterator
    | Type | Definition | | ------------------- | --------------------------------------------------------------------------------------------- | | value_type | basic_node | | reference | const basic_node& | | pointer | const basic_node* | | difference_type | std::ptrdiff_t | | iterator_category | std::bidirectional_iterator_tag |

Member Functions

Name Description
key Returns const reference to a key node of the current key-value pair.
fkyaml::exception is thrown if an iterator points to a sequence element.
value Returns reference to a sequence element if an element points to a sequence element, or a mapping value otherwise.

Non-Member Functions

Name Description
get Returns reference to a key (if I == 0) or a value (if I == 1) mainly for structured binding support.
fkyaml::exception is thrown if an iterator points to a sequence element.

Examples

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

int main() {
    // create a container node.
    fkyaml::node sequence_node = {1, 2, 3};
    // get an iterator to the first sequence element.
    fkyaml::node::iterator it = sequence_node.begin();
    fkyaml::node::const_iterator end_it = sequence_node.cend();

    // print all the elements.
    // note `iterator` and `const_iterator` are comparable.
    for (; it != end_it; ++it) {
        std::cout << *it << std::endl;
    }

    it = sequence_node.begin();
    try {
        // key() cannot be called on an iterator pointing to a sequence element.
        std::cout << it.key() << std::endl;
    }
    catch (const fkyaml::exception& e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

output:

1
2
3
Cannot retrieve key from non-mapping iterators.

See Also