Boost Ptree Custom Sort - PullRequest
       44

Boost Ptree Custom Sort

0 голосов
/ 27 июня 2019

Я хочу использовать эту функцию и создать собственную функцию сортировки, но я не знаю, как работает функция сравнения или с лямбда-выражением.

Может ли кто-то быть так любезен, чтобы дать мне рабочий пример дляиспользуя его.

/* Sorts the direct children of this node according to the predicate.
   The predicate is passed the whole pair of key and child. */

   template <class Compare> void sort (Compare comp);

Предикату передается вся пара ключ и дочерний элемент.

Это пара ключ / дерево?Я не знаю, каков тип данных.

Я ищу что-то вроде лямбда-функции, равной для списков

  unorderedGenList.sort([](const boost::property_tree::ptree & treeA, const boost::property_tree::ptree & treeB)
    {   
        if(std::stod(treeA.get<std::string>("sortA","0")) < std::stod(treeB.get<std::string>("sortA","0")) 
          || (std::stod(treeA.get<std::string>("sortA","0")) == std::stod(treeB.get<std::string>("sortA","0")) && std::stod(treeA.get<std::string>("sortB","0")) < std::stod(treeB.get<std::string>("sortB","0")))
        )
          return true;
        return false;
    });

1 Ответ

1 голос
/ 28 июня 2019

Я думаю, что получил решение

typedef std::pair< const Key, self_type >    value_type; 

unorderedPtree.sort([](const boost::property_tree::value_type& treeA, const boost::property_tree::value_type & treeB){
   treeA.second.get<std::string>("sortB","0")) ...
}
...