当前位置: 首页 > 知识库问答 >
问题:

获取值时出现boost property_tree问题

石正卿
2023-03-14

这是我第一次体验提升::property_tree我找不到一种方法来重现从留档(如何访问属性树中的数据)之后的树中获取值的方法。这是我为尝试属性树而编写的简单代码:

#include <iostream>
#include <string>

#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ptree.hpp>

namespace pt = boost::property_tree;

int main(int argc, char *argv[]) {
  pt::ptree tree;

  tree.put("pi", 3.14159);
  tree.put("name", "John Doe");

  for (auto &[key, value] : tree)
    std::cout << key << " : " << value.get_value<std::string>() << "\n";

  std::cout << "pi : " << tree.get_value("pi") << "\n";
  std::cout << "name : " << tree.get_value("name") << "\n";

  auto pi = tree.get_optional<float>("pi").get();
  std::cout << "pi optional : " << pi << "\n";

  auto pi_found = tree.find("pi");
  std::cout << "pi found : " << pi_found->second.data() << "\n";

  // the commented line doesn't compile
  // std::cout << "not found : " << tree.get_value<int>("null") << "\n";

  std::cout << "not found : " << tree.get_value("null") << "\n";

  // the line below causes an assertion error:
  // Assertion failed: (this->is_initialized()), function get, file /usr/local/include/boost/optional/optional.hpp, line 1191.
  // not found : Abort trap: 6
  std::cout << "not found : " << tree.get_optional<int>("null").get() << "\n";

  pt::write_info("ptree.info", tree);

  return 0;
}

这是输出:

pi : 3.1415899999999999
name : John Doe
pi :
name :
pi optional : 3.14159
pi found : 3.1415899999999999
not found :

如<code>树所示。get_value(“whather”)在树中不返回值。get_value(“null”)不引发异常,并且<code>get_optional

我的环境是:

MacOS 10.11.6
macbrew installed tools and libraries
boost 1.67
clang 7.0
meson build system

共有2个答案

连厉刚
2023-03-14

我的错,而不是使用<code>tree.get

龚鸿雪
2023-03-14

您可以将ptree绘制为:

node1 is tree (has 2 children, data() of tree is "")
 |
 |- (node2) pi ----> data=3.14...
 |
 |- (node3) name --> data="Joe Doe"

[1]

可以看出tree.get_value(“无论”)不返回值

tree是节点,有2个孩子(pi,name)。在调用时

tree.get_value(defaultValue) // you are not passing PATH, but DEFAULT VALUE

上面的行被翻译成

tree.get_child("").get_value(defaultValue)

因此,路径存在,因为它是指向node和tree的路径。data()返回此路径的-空字符串。因此,无法打印<code>defaultValue的参数是默认值。因此,替换

  std::cout << "pi : " << tree.get_child("pi").get_value("PI is 4.0") << "\n";
  std::cout << "name : " << tree.get_child("name").get_value("noname") << "\n";

你会看到< code>3.14和< code >乔·多伊。

[2]

tree.get_value("null ")不会抛出异常

如[1]点所述“”路径存在,此路径的<code>data()

[3]

//注释行不编译// std::cout

这一行无法编译,因为ptree类没有那个方法,我想你想调用这个方法:

  template<typename Type> 
  unspecified get_value(const Type & default_value) const;

您将Type定义为intint作为函数模板参数涉及默认值只能是int,而不是字符串。

 类似资料:
  • 我在获取下拉列表的值时遇到了麻烦,我有学生实体和部分实体,它们之间有一个关系船,在jsp中它就像而不是这个在jsp中,我需要获得特定的bean属性名称,如 学生示范班 我的道实现是这样的 像金丝马龙一样。 控制器就像 我对这个问题的最后润色是如何获得指定bean属性名的下拉值(这里我想获得bean属性名称,如)com.chan.Eschool.student.model.Section@26552

  • 我对这段代码有一个问题: 当我保存它时,我会得到以下错误代码: 未捕获的ReferenceError:未定义birthDate 因此,基本上我正在训练获取值“05June 2001”,以便将其作为参数传递给Date函数,但我不能这样做,不过我可以从浏览器控制台访问该值,在那里我确实犯了错误,并提前表示感谢。

  • 我想获取存储在JWT中的角色声明。我使用以下代码来做到这一点: 但这最终给出了目标代码: 我调试了代码,发现了这样的声明: 如何提取“ROLE_ADMIN”?

  • 我可以用水槽获取推文,但是,流式传输的语言不是我想要的。下面是flume.conf文件 我收到的推文如下所示: 有人能建议我需要做的改变吗?

  • 我正在尝试使用Boost来解析JSON文件。这是 JSON 文件 和一个MWE 问题:我目前知道如何读取最外层的变量<code>a。然而,我在阅读其他级别(如<code>b_a、b_b_a和b_d_a</code>等)时遇到了困难。如何使用Boost实现这一点?我不一定要寻找涉及循环的解决方案,只是想弄清楚如何“提取”内部变量。 如果其他库是最佳的,我愿意使用它们。但是到目前为止,Boost在我看

  • 问题内容: 我没有收到来自服务器的JSON类型数据作为响应。 我正在使用JSON插件。 如果我没有得到任何回应,但是如果我没有提及任何响应,那么我正在获取页面的HTML格式。 struts.xml: JSP页面: 控制台错误: org.apache.struts2.dispatcher.Dispatcher- 无法找到动作或结果没有为动作action.Part和结果定义结果。{和“ col1”:“