Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c++;,可能使用boost::property_tree::ptree?_C++_Boost_Boost Spirit - Fatal编程技术网

C++ 在c++;,可能使用boost::property_tree::ptree?

C++ 在c++;,可能使用boost::property_tree::ptree?,c++,boost,boost-spirit,C++,Boost,Boost Spirit,我的任务很简单-我只需要解析这样的文件: Apple = 1 Orange = 2 XYZ = 3950 但我不知道那套可用的钥匙。我使用C#解析这个文件比较容易,让我演示一下源代码: public static Dictionary<string, string> ReadParametersFromFile(string path) { string[] linesDirty = File.ReadAllLines(path);

我的任务很简单-我只需要解析这样的文件:

Apple = 1
Orange = 2
XYZ = 3950
但我不知道那套可用的钥匙。我使用C#解析这个文件比较容易,让我演示一下源代码:

    public static Dictionary<string, string> ReadParametersFromFile(string path)
    {
        string[] linesDirty = File.ReadAllLines(path);
        string[] lines = linesDirty.Where(
            str => !String.IsNullOrWhiteSpace(str) && !str.StartsWith("//")).ToArray();

        var dict = lines.Select(s => s.Split(new char[] { '=' }))
                        .ToDictionary(s => s[0].Trim(), s => s[1].Trim());
        return dict;
    }
但是不可能反复讨论它,请参考这个问题


问题是,在C++上编写C++代码的最简单的方法是什么?

,我已经简化了一点,省去了注释的逻辑(不管怎么说,它看起来都坏了)。
#包括
#包括
#包括
#包括
typedef std::成对输入;
//这是官方不允许的(这是一个过载,不是一个专门化),但它是
//我知道的每一个编译器都很好。
名称空间标准{
std::istream&operator>>(std::istream&is,entry&d){
std::getline(is,d.first,'=');
std::getline(is,d.second);
回报是;
}
}
int main(){
//打开一个输入文件。
std::ifstream-in(“myfile.ini”);
//将文件读入我们的地图:
std::map dict((std::istream_迭代器(in)),
std::istreamu迭代器();
//展示我们阅读的内容:
用于(输入常量和e:dict)

std::cout直接回答您的问题:当然可以迭代属性树

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

int main()
{
    using boost::property_tree::ptree;
    ptree pt;

    read_ini("input.txt", pt);

    for (auto& section : pt)
    {
        std::cout << '[' << section.first << "]\n";
        for (auto& key : section.second)
            std::cout << key.first << "=" << key.second.get_value<std::string>() << "\n";
    }
}

我用以前的方法编写了一个功能非常全面的文件解析器:

它支持注释(单行和块)、引号、转义符等

(作为奖励,它可以选择记录所有解析元素的确切源位置,这是该问题的主题)


不过,出于您的目的,我想我还是推荐使用Boost属性树。

当然可以在ptree上进行迭代,事实上这很简单,因为我的答案现在显示(更新)(注意:program options!=属性树)谢谢,我可以进行迭代。但是由于某些原因,没有打印值。例如,我只看到[Cat1][Catu 2][Catu 3]作为输出,没有“name1”和“name2”等。稍后我将尝试解决此问题,但如果您知道解决方案,我将感谢您的帮助:)“不过,出于您的目的,我想我建议编译Boost Property Tree。”-我不明白你到底建议什么。你建议不要使用
ini\u解析器
?你建议使用
xml
而不是
ini
还是什么?@javapowered你知道,我停在那里是因为你要求这样做。当然,你也可以打印值。我已经添加了它now@javapowered请不要而不是代码> \n除非你知道你在做什么。在这种情况下,它是在[S]中。我总是使用<代码> STD::EntL\/Cord>而不是\n来遵循C++风格。我在这里读到,N必须更快,但是使用的人“
#include <map>
#include <fstream>
#include <iostream>
#include <string>

typedef std::pair<std::string, std::string> entry;

// This isn't officially allowed (it's an overload, not a specialization) but is
// fine with every compiler of which I'm aware.
namespace std {
std::istream &operator>>(std::istream &is,  entry &d) { 
    std::getline(is, d.first, '=');
    std::getline(is, d.second);
    return is;
}
}

int main() {
    // open an input file.
    std::ifstream in("myfile.ini");

    // read the file into our map:
    std::map<std::string, std::string> dict((std::istream_iterator<entry>(in)),
                                            std::istream_iterator<entry>());

    // Show what we read:
    for (entry const &e : dict) 
        std::cout << "Key: " << e.first << "\tvalue: " << e.second << "\n";
}
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

int main()
{
    using boost::property_tree::ptree;
    ptree pt;

    read_ini("input.txt", pt);

    for (auto& section : pt)
    {
        std::cout << '[' << section.first << "]\n";
        for (auto& key : section.second)
            std::cout << key.first << "=" << key.second.get_value<std::string>() << "\n";
    }
}