C++ 使用常量迭代器放置/擦除属性树,或如何将常量迭代器转换为迭代器
我在ubuntu 12.04lts和Clang3.4上使用Boost1.55.0 我有一个C++ 使用常量迭代器放置/擦除属性树,或如何将常量迭代器转换为迭代器,c++,xml,boost,iterator,boost-propertytree,C++,Xml,Boost,Iterator,Boost Propertytree,我在ubuntu 12.04lts和Clang3.4上使用Boost1.55.0 我有一个boost::property_tree::ptree,其xml输入如下所示: <root> <persons> <person> <name>dad</name> <age>43</age> </person>
boost::property_tree::ptree
,其xml输入如下所示:
<root>
<persons>
<person>
<name>dad</name>
<age>43</age>
</person>
<person>
<name>me</name>
<age>4</age>
</person>
</persons>
</root>
boost::property_tree::ptree pt;
boost::property_tree::read_xml(inputFileName, pt);
boost::property_tree::ptree& persons = pt.get_child("root");
for(boost::property_tree::ptree::const_iterator it = persons.begin(); it != persons.end(); ++it)
{
std::string name = it->second.get<std::string>("name");
if(name == "dad")
// erase that name node from pt
persons.erase(it->second.find("name"); // this doesn't work
}
爸爸
43
我
4.
因此,我有一个具有相同标记的节点列表
为了读取它们,我在树上迭代,并根据需要擦除节点的条件。这看起来像:
<root>
<persons>
<person>
<name>dad</name>
<age>43</age>
</person>
<person>
<name>me</name>
<age>4</age>
</person>
</persons>
</root>
boost::property_tree::ptree pt;
boost::property_tree::read_xml(inputFileName, pt);
boost::property_tree::ptree& persons = pt.get_child("root");
for(boost::property_tree::ptree::const_iterator it = persons.begin(); it != persons.end(); ++it)
{
std::string name = it->second.get<std::string>("name");
if(name == "dad")
// erase that name node from pt
persons.erase(it->second.find("name"); // this doesn't work
}
boost::property_tree::ptree pt;
boost::property_tree::read_xml(inputFileName,pt);
boost::property_tree::ptree&persons=pt.get_child(“根”);
for(boost::property_tree::ptree::const_迭代器it=persons.begin();it!=persons.end();+it)
{
std::string name=it->second.get(“name”);
如果(姓名=“爸爸”)
//从pt中删除该名称节点
persons.erase(it->second.find(“name”);//这不起作用
}
[Edit]根据pmr的假设,我编写了以下代码:
boost::property_tree::ptree::iterator i = persons.begin();
auto assoc_i = it->second.find("name");
auto ci = persons.to_iterator(assoc_i);
std::advance(i, std::distance<boost::property_tree::ptree::const_iterator>(iterator, ci)); // --> here it hangs
persons.erase(i);
boost::property_tree::ptree::iterator i=persons.begin();
自动关联i=it->second.find(“名称”);
自动ci=个人到迭代器(关联);
std::advance(i,std::distance(迭代器,ci));//-->它挂在这里
人。删除(i);
现在它编译了,应用程序没有崩溃,但它挂起在上面提到的位置。我不知道为什么。[/Edit]
预先感谢。 C++ C++ 11的API API指定了一个成员函数<代码>迭代器容器::擦除(CONSTATIORATIOR,CONTRONATIORATER)。不幸的是,<代码> Basic pPruts不这样做,所以你被旧的C++方式转换成了<代码> CONTRORATIONATOR <代码>到<代码>迭代器< /代码>:
// tree is your ptree, my_const_iter a ptree::const_iterator
ptree::iterator i = tree.begin();
advance (i, std::distance<ConstIter>(i,my_const_iter));
//树是你的ptree,我的常量是ptree::const\u迭代器
ptree::迭代器i=tree.begin();
前进(i,标准::距离(i,我的常数));
您手头的问题与迭代器的常量无关,您只是在用错误的ptree迭代器擦除
ptree& persons = pt.get_child("root.persons");
for(auto it = persons.begin(); it != persons.end();) {
if(it->second.get<string>("name") == "dad")
it = persons.erase(it);
else
++it;
}
@DieterLücking在这里看到签名2:谢谢。但现在我有另一个问题。我在编辑我的问题时解释了。你知道,为什么应用程序现在挂起吗?@Rico-E我真的建议每个问题只保留一个主题。现在很难看到begin问了什么。你可能还想拆分语句的子操作以获得ex在程序挂起的位置执行操作,并在调试器中查看它。@pmr谢谢。我删除了所有其他问题,并将代码拆分为子操作(编辑了我的文章)。std::advance仍然无法工作。因为我使用的是clang,所以我没有调试器。或者是否有clang调试器?您可以使用GDB(最好是7+)或LLDB(因为6.3附带Xcode)。我不知道为什么你会认为像clang这样广泛使用的编译器工具链不支持与你期望的相同的调试工具。即使现在它也支持在Windows上与Visual Studio的有限集成。真的令人印象深刻。