- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
"A": "1"
"A.B": "2"
"A.C": "3"
如何获取A.B
的值如果我遍历 ptree 它工作。如果我尝试获得 pt.get_child("A\.B").get_value<std::string>()
的值(value).我得到以下异常
terminate called after throwing an instance of boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::ptree_bad_path> >'
what(): No such node
请在下面找到完整的代码
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
#include <string>
#include <iterator>
using boost::property_tree::ptree;
/* Indent Json Output */
std::string indent(int level) {
std::string s;
for (int i = 0; i < level; i++) s += " ";
return s;
}
/* Print tree in json format */
void printTree(ptree & pt, int level) {
if (pt.empty()) {
std::cerr << "\"" << pt.data() << "\"";
} else {
if (level) std::cerr << std::endl;
std::cerr << indent(level) << "{" << std::endl;
for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
std::cerr << indent(level + 1) << "\"" << pos-> first << "\": ";
printTree(pos->second, level + 1);
++pos;
if (pos != pt.end()) {
std::cerr << ",";
}
std::cerr << std::endl;
}
std::cerr << indent(level) << " }";
}
return;
}
int main()
{
ptree pt;
read_ini("sample.ini", pt);
printTree(pt, 0);
std::cout << pt.get_child("A.B").get_value<std::string>() << std::endl; //tries to resolve A.B to two nodes
std::cout << pt.get_child("A\\.B").get_value<std::string>() << std::endl; //error
}
示例.ini
A=1
A.B=2
A.C=3
最佳答案
您可以使用替代路径定界符,但这有点棘手,而且没有很好的记录。
您必须临时指定一个备用路径分隔符:
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
int main() {
ptree pt;
pt.put("a.b", "first");
pt.put(ptree::path_type("a|complicated.name", '|'), "second");
write_ini(std::cout, pt);
}
打印
[a]
b=first
complicated.name=second
关于c++ - ptree get_value 名称包括 ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50287301/
我有一段代码可以遍历 boost 属性树 (XML)。 我需要当前节点的 ptree,而不是节点的子节点。 更新 xml树 child A1 child A2
我想在 C++ 中序列化分层数据结构。我正在处理的项目使用 boost,所以我使用 boost::property_tree::ptree 作为我的数据节点结构。 我们有像 Person 这样的高级结
首先我要说我认为我知道应该怎么做,但是我的代码不会以我尝试的任何方式编译。我的假设基于 this official example of empty ptree trick .在那里你可以找到下一行:
我想制作一个系统,将 XML 文件中的选项加载到一个 ptree 中,并跨多个线程访问这个 ptree。到目前为止,我有一个简单的类,每个线程都可以访问它,它包含方法 put(id) 和 get()。
我正在使用 boost 库来解析如下所示的 .ini 文件: [head] type1=val1 type2=cal2 ... 我的代码是这样的: #include #include #inclu
我想知道是否有一种方法可以通过遍历父节点的 vector 来获取子节点中的数据。我有一个我计划经常更改的 XML 文件,因此我想避免对属性名称进行硬编码。因此,我想在我的子节点中提取数据而不使用 pt
"A": "1" "A.B": "2" "A.C": "3" 如果我通过ptree进行迭代,如何获得A.B的值。如果我尝试 获得pt.get_child("A\.B").get_value()的值。我
我正在使用 boost 库来操作 JSON 字符串,我想访问第一个元素。 我想知道是否有一些方便的方法可以访问没有路径名的 ptree 的第一个元素。 我这样做了,但我没有得到任何值(value):
"A": "1" "A.B": "2" "A.C": "3" 如何获取A.B的值如果我遍历 ptree 它工作。如果我尝试获得 pt.get_child("A\.B").get_value() 的值(
我正在使用 boost 库来操作 JSON 文件,我想访问此 JSON 中数组的特定索引。 boost::property_tree::ptree& jsonfile; const boost::pr
我想让 write_json 输出一个顶级数组,效果如下: [{...},{...},{...},...,{...}] 但是当我将列表传递给 write_json 时,它会转换为充满空白键的 json
在以下使用 C++ Boost 属性树的代码中,我希望得到一个漂亮的输出,例如 { "fruit": { "apple": "true", "orange": "true" }
我有一个配置文件,它是一个json。我创建了一个类 (ConfigFile) 来读取该文件并存储值(使用 boost 解析器和 ptree)。我在徘徊,将 ptree 用作 ConfigFile 类的
我使用以下代码创建一个数字数组。 运行以下代码后,我收到以下结果: { "": "1.100000", "": "2.200000", "": "3.300000" } 这很好
我有一个带有节点的 boost ptree: pt.put("a.b", 1.0); pt.put("a.c", 2.0); pt.put("b.g", 3.0); 我想提取一棵具有“a.b”和“a.
我开始使用 boost 的 ptree 和 json 解析器来保存一些快速信息。问题是我只需要保存一些 URI,所以我不关心 key 。现在我想找到某个值并将其删除。我该怎么做 { "MyAr
假设我的目标是创建以下形式的 xml: 我有以下代码: ptree pt; pt.put("main",""); ptree temp1; temp1.
在 boost 属性树中,我想将 a.b.c2 等键重命名为 a.b.c3。 一种方法是删除节点并用另一个名称放置它的拷贝。问题在于该节点位于其其他兄弟节点的末尾。我更喜欢保持秩序。我该如何修复此代码
我的 JSON 是这样的: { "apps":[ { "id":"x", "val":"y", } ]
我发现 boost::property_tree::ptree 有巨大的内存开销。我的估计是一个空的 ptree 大约有 150 个字节,并且,放入 ptree 中的任何条目至少增加了 150 个字节
我是一名优秀的程序员,十分优秀!