C++:提升 ptree 删除子项:无匹配功能
C++: boost ptree remove children: no matching function
为了删除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"的所有节点及其子节点。
不幸的是,ptree
api 没有像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_type
或iterator
来.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();
相关文章:
- Rcpp并行无匹配函数,用于调用"转换"
- C++,调用'filter'没有匹配功能
- QT 连接没有要调用的匹配功能
- 运算符重载错误无匹配运算符<<
- 为什么通过带有文字编号的引用调用会出现"无匹配函数"错误?
- C++:提升 ptree 删除子项:无匹配功能
- 无匹配函数呼叫getline()
- 错误:呼叫构造器的匹配函数无匹配功能
- Qt - 没有用于连接的匹配功能
- C 线程 - 无匹配函数供呼叫
- RCPP - 调用'transform'没有匹配功能
- 将对象传递到类,没有接收匹配功能错误
- C 汇编错误:无匹配的构造函数以进行初始化
- 使用正则匹配功能定义
- 如何使用无主功能的片段着色器
- 操作员[](STD :: vector)无匹配
- 错误代码:无匹配函数供呼叫
- 错误:如果(object1 == object2),“操作器==”无匹配(操作数类型为“ const class”和“
- 为链接列表构建迭代器类(错误:无匹配的构造函数以初始化)
- GetLine C 无匹配功能