使用 boost::program_options 和 push_back 读取 std::vector 吗?
我有一个配置文件,其中包含端点条目列表。每个条目都标有标题 [endpt/n](对于第 n 个端点),并由 MAC 和 IP 地址组成。我想使用 boost::program_options 将地址读取为字符串,并将结果 Push_back 到两个向量上。我已经浏览了program_options文档,但我无法找到我正在寻找的内容...这是端点条目的示例:
[endpt/2]
mac=ff-22-b6-33-91-3E
ip=133.22.32.222
这是我当前用于添加每个端点的MAC和IP的代码boost::options_description 的选项:
std::vector<std::string> mac(NUM_ENDPTS);
std::vector<std::string> ip(NUM_ENDPTS);
for(int e = 0; e < NUM_ENDPTS; e++)
{
//convert endpoint 'e' to a string representing endpoint heading
std::stringstream tmp; tmp.clear(); tmp.str(""); tmp << e;
std::string strEndpt = tmp.str();
std::string heading = "endpt/"+strEndpt;
cfig_file_options.add_options()
((heading+".mac").c_str(), po::value<std::string>(&mac[e]), ("ENDPT MAC")
((heading+".ip").c_str(), po::value<std::string>( &ip[e]), ("ENDPT IP")
;
}
po::variables_map vm;
po::store(po::parse_config_file(config_stream, cfig_file_options), vm);
po::notify(vm);
这段代码工作正常,但由于一些原因,我想为 MAC 和 IP 地址声明空向量,并在 boost 读取它们时将选项推回它们。我是 Boost 的新手,因此任何有关更好的方式阅读列表的建议或任何其他帮助将不胜感激。谢谢!
I have a configuration file which contains a list of endpoint entries. Each entry is labeled with a heading of [endpt/n] (for the nth endpoint), and consists of a MAC and IP address. I'd like to use boost::program_options to read the addresses as strings, and push_back the results onto two vectors. I've looked through the program_options documentation, but I haven't been able to find what I'm looking for... Here's an example of an endpoint entry:
[endpt/2]
mac=ff-22-b6-33-91-3E
ip=133.22.32.222
Here's the code I'm currently using to add each endpoint's MAC and IP options to the boost::options_description:
std::vector<std::string> mac(NUM_ENDPTS);
std::vector<std::string> ip(NUM_ENDPTS);
for(int e = 0; e < NUM_ENDPTS; e++)
{
//convert endpoint 'e' to a string representing endpoint heading
std::stringstream tmp; tmp.clear(); tmp.str(""); tmp << e;
std::string strEndpt = tmp.str();
std::string heading = "endpt/"+strEndpt;
cfig_file_options.add_options()
((heading+".mac").c_str(), po::value<std::string>(&mac[e]), ("ENDPT MAC")
((heading+".ip").c_str(), po::value<std::string>( &ip[e]), ("ENDPT IP")
;
}
po::variables_map vm;
po::store(po::parse_config_file(config_stream, cfig_file_options), vm);
po::notify(vm);
This code works fine, but for a few reasons, I'd like to declare empty vectors for the MAC and IP addresses, and push_back the options onto them as boost reads them. I'm new to Boost, so any suggestions on better ways to read a list, or any other help would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完全按照您想要的方式进行操作非常简单。首先,使用 po::value<向量< std::string > > 而不是
po::value< std::string >
作为程序选项提供向量的特殊支持。然后,直接引用您的两个向量,如下所示。这里的关键是所有 mac 和 ip 地址都使用公共向量进行存储。我应该指出,这不一定会将 ini 文件中的 endpt 编号与向量中的正确索引相关联,除非文件中维护严格的顺序。
It is straightforward to do exactly as you want. First, use
po::value< vector< std::string > >
instead ofpo::value< std::string >
as program-options provides special support of vectors. Then, reference your two vectors directly asThe key here is that all mac and ip addresses use common vectors for storage. I should point out that this won't necessarily associate the endpt number in the ini file with the correct index in the vector, unless a strict order is maintained in the file.