gpt4 book ai didi

c++ - Boost ptree 数字数组

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:24 26 4
gpt4 key购买 nike

我使用以下代码创建一个数字数组。

运行以下代码后,我收到以下结果:

{
"": "1.100000",
"": "2.200000",
"": "3.300000"
}

这很好,除了我想要的结果必须是数字数组而不是字符串。通过 boost::property_tree::ptree(x) 直接添加一个数字也会给我一个错误。如何生成我的输出 json 结果?

{
"": 1.100000,
"": 2.200000,
"": 3.300000
}

代码:

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

int main()
{
boost::property_tree::ptree pt;
std::vector<double> Vec={1.1,2.2,3.3};
for(double x:Vec)
{
std::string x_string=std::to_string(x);
pt.push_back(
std::make_pair("",
boost::property_tree::ptree(x_string)) );

}
boost::property_tree::json_parser::write_json(std::cout, pt);
std::cout<<std::endl;
return 0;
}

最佳答案

PTree 没有这样的功能。

一切都是序列化格式的文本。即使选择的后端格式可以支持(有限的)类型化数据。

文档 proof :

enter image description here

正如我不断重申的那样:

Boost does not have an XML library.

Boost does not have a JSON library.

Boost has a Property Tree library. It deals with Property Trees. Not JSON, XML or whatever else.

关于c++ - Boost ptree 数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41609939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com