提升ptree数字数组

编程入门 行业动态 更新时间:2024-10-09 20:29:12
本文介绍了提升ptree数字数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

{: 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; 返回0; }

解决方案

PTree没有此类功能。

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

文档

我一直在重申:

Boost没有没有具有XML库。

Boost没有没有具有XML库。 JSON库。

Boost有一个属性树库。它处理属性树。不是JSON,XML或其他任何格式。

I use the following code to create an array of the numbers.

After running the following code, I reveive the following results:

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

It is good except for my desired result has to be an array of numbers not string. Adding a number directly by boost::property_tree::ptree(x) gives me an error too. How can I produce my output json results?

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

Code:

#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 has no such features.

Everything is text in the serialized formats. Even if the chosen backend format could support (limited) typed data.

Documentation proof:

As I keep re-stating:

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.

更多推荐

提升ptree数字数组

本文发布于:2023-11-30 18:21:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   数字   ptree

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!