提升ptree数字数组 [英] Boost ptree array of numbers

查看:143
本文介绍了提升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数字数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆