c – Boost property_tree – 使用简单的数组或容器
发布时间:2020-12-16 09:32:32 所属栏目:百科 来源:网络整理
导读:我正在使用boost property_tree加载一个ini文件.我的ini文件主要包含“简单”类型(即字符串,整数,双精度等),但我确实有一些表示数组的值. [Example]thestring = stringtheint = 10theintarray = 1,2,3,4,5thestringarray = cat,dog,bird 我无法弄清楚如何通
我正在使用boost property_tree加载一个ini文件.我的ini文件主要包含“简单”类型(即字符串,整数,双精度等),但我确实有一些表示数组的值.
[Example] thestring = string theint = 10 theintarray = 1,2,3,4,5 thestringarray = cat,dog,bird 我无法弄清楚如何通过编程方式将theintarray和thestringarray加载到像vector或list这样的容器对象中.我注定要把它作为一个字符串读出并自己解析出来吗? 谢谢! 解决方法
是的,你注定要自己解析.但它相对容易:
template<typename T> std::vector<T> to_array(const std::string& s) { std::vector<T> result; std::stringstream ss(s); std::string item; while(std::getline(ss,item,',')) result.push_back(boost::lexical_cast<T>(item)); return result; } 比可以使用的: std::vector<std::string> foo = to_array<std::string>(pt.get<std::string>("thestringarray")); std::vector<int> bar = to_array<int>(pt.get<std::string>("theintarray")); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |