问题描述
我加载与升压property_tree ini文件。我的ini文件中大多含有简单类型(即字符串,整型,双打,等等),但我确实有一些价值的重新present数组。
I'm loading an ini file with boost property_tree. My ini file mostly contains "simple" types (i.e., strings, ints, doubles, etc.) but I do have some values that represent an array.
[Example]
thestring = string
theint = 10
theintarray = 1,2,3,4,5
thestringarray = cat, dog, bird
我无法搞清楚如何获得提振programmagically负载 theintarray
和 thestringarray
成容器对象像矢量
或列表
。难道我注定要刚读它作为一个字符串和解析它自己吗?
I'm having trouble figuring out how to get boost to programmagically load theintarray
and thestringarray
into a container object like vector
or list
. Am I doomed to just read it in as a string and parse it out myself?
谢谢!
推荐答案
是的,你注定要在自己的解析。但它是相对容易的可能:
Yes you are doomed to parse on your own. But it's relatively easy possible:
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"));
这篇关于升压property_tree - 简单数组或容器中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!