C++:提升 ptree 删除子项:无匹配功能

C++: boost ptree remove children: no matching function

本文关键字:无匹配 功能 删除 提升 ptree C++      更新时间:2023-10-16

为了删除boost属性树的子级,我在erase函数中使用了一个直接节点,这导致

error: no matching function for call to 
‘boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, 
std::__cxx11::basic_string<char> >::erase(std::pair<const 
std::__cxx11::basic_string<char>, 
boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, 
std::__cxx11::basic_string<char> > >&)’

pt0.erase(pt_child);

代码的正确形式是什么?

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace boost::property_tree;
void print(const ptree &p)
{
json_parser::write_json(std::cout, p);
}
int main()
{
ptree pt0;
for(int i=0;i<10;i++)
pt0.put_child("kid"+std::to_string(i+1),ptree());
print(pt0);
for(auto& pt_child : pt0)
pt0.erase(pt_child);
print(pt0);
return 0;
}

你可以这样做:ptree.get_child("path.to"(.erase("child"(;请注意,这将删除路径"path.to"中名为"child"的所有节点及其子节点。

不幸的是,ptreeapi 没有像get_child那样与property_tree::path一起使用的remove_child方法。不过,这里有一个小方法可以做到这一点。重要的部分是您可以使用点表示法来指定要删除的任何子项。此外,此实现仅删除一个子项。

#include <boost/property_tree/ptree.hpp>
#include <boost/algorithm/string.hpp>
// TODO: this should support boost::property_tree::path
// like get_child does to make it obvious that it supports
// the path separator notation for specifying sub children
bool remove_child(boost::property_tree::ptree& pt, const std::string& path) {
// split up the path into each sub part
std::vector<std::string> path_parts;
boost::split(path_parts, path, boost::is_any_of("."));
// check each part of the path
auto* root = &pt;
for (const auto& part : path_parts) {
// if we dont have this sub child bail
auto found = root->find(part);
if (found == root->not_found())
return false;
// if this was the last one to look for remove it
if (&part == &path_parts.back()) {
root->erase(root->to_iterator(found));
}// next sub child
else {
root = &found->second;
}
}
// made it to the last sub child without bailing on not found
return true;
}

根据文档,您只能通过key_typeiterator.erase,但您正在尝试通过value_type来做到这一点。

你可以做任何一件事

for(auto& [key, data]: pt0)
pt0.erase(key);

或显式循环迭代器:

for(auto it = pt0.begin(); it != pt0.end(); ++it)
pt0.erase(it);

但是,由于您无论如何都要删除所有孩子,因此更好的方法是仅

pt0.clear();