Ptree acceleration with reverse iterators

The following code works correctly:

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <string>

using namespace boost::property_tree;

int main()
{
    ptree root;
    root.put("building.age", "42");
    root.put("company.age", "32");
    root.put("street.age", "19");

    ptree attached_node;
    attached_node.put("confirmed","yes");
    attached_node.put("approved","yes");

    for(auto it=root.begin();it!=root.end();++it)
    {
        std::cout
                << (it->first)
                << ": "
                << (it->second.get<std::string>("age"))
                << std::endl;
        if(it->first=="company")
            root.insert(it,make_pair("conditions",attached_node));
    }
    return 0;
}

However, as soon as I repeat the postback through:

    for(auto it=root.rbegin();it!=root.rend();++it)

I am facing an error:

 error: no matching function for call to ‘boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::insert(boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::reverse_iterator&, std::pair<const char*, boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >)’
     root.insert(it,make_pair("conditions",attached_node));
                                                         ^

How can I fix this problem?

+4
source share
1 answer

This is because the insert function does not accept a reverse iterator.

Use base()to get it:

enter image description here

root.insert(it.base(), make_pair("conditions",attached_node));

BOOM: inifite loop! . never . Undefined , company node "next" .

.

" break;".

: CQS.

Live On Coliru

auto it = find_by_key(root.rbegin(), root.rend(), "company");
if (it != root.rend())
    root.insert(it.base(), make_pair("conditions",attached_node));

, ! find_by_key - :

template <typename It>
It find_by_key(It f, It l, std::string const& key) {
    return std::find_if(f, l, [&](auto const& pair) {
        //std::cout << pair.first << ": " << pair.second.get("age", "?") << "\n";
        return pair.first == key;
    });
}

, ptree:

Live On Coliru

auto it = root.equal_range("company").second;
if (it != root.not_found())
    root.insert(root.to_iterator(it), make_pair("conditions",attached_node));

:

, , .

: " for for" -

enter image description here

+5

Source: https://habr.com/ru/post/1684213/


All Articles