在程序选项中添加键值对

时间:2016-02-12 17:06:07

标签: c++ dictionary command-line boost-program-options

我需要在程序中加载一个带有整数键和字符串值的映射,如下所示:

1, oil
2, car
5, house

我想使用boost::program_options加载它们。我可以在tutorial中看到我可以使用类似这样的语法从选项加载矢量:

int opt;
po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("optimization", po::value<int>(&opt)->default_value(10), "optimization level")
    ("include-path,I", po::value< vector<string> >(), "include path")
    ("input-file", po::value< vector<string> >(), "input file");

然后我可以使用program --input-file file1, --input-file file2来创建包含file1file2的向量。从程序选项中实现地图的最佳方法是什么?

我可以使用例如字符串然后拆分字符串以获取值,例如program --pair "1, oil" --pair "2, car" --pair "5, house",或者我可以使用program --key 1 --value oil --key 2 --value car --key 5 --value car将它们分开。

我想使用可以从命令行和配置文件轻松编写的内容。哪种方法最好?

1 个答案:

答案 0 :(得分:1)

  

我可以使用例如字符串然后拆分字符串以获取值,例如videoLayer.frame = CGRectMake(0, 0, videoSize.height, videoSize.width) //notice the switched width and height ... videoComp.renderSize = CGSizeMake(videoSize.height,videoSize.width) //this make the final video in portrait ... layerInstruction.setTransform(videoTrack.preferredTransform, atTime: kCMTimeZero) //important piece of information let composition know you want to rotate the original video in output ...

我坚持使用第一个选项并阅读program --pair "1, oil" --pair "2, car" --pair "5, house"字符串向量。然后,您将浏览本机解析的矢量,拆分字符串并填充地图。

  

...或者我可以使用程序将它们分开 - 键1 - 值油 - 键2 - 值车 - 键5 - 值车

我不确定这种方式会保留你需要的强大顺序。

相关问题