2011-02-13 57 views
3

我使用boost property_tree加载了一个ini文件。我的ini文件大多包含“简单”类型(即字符串,整数,双精度等),但我确实有一些代表数组的值。Boost property_tree - 使用简单的数组或容器

[Example] 
thestring = string 
theint = 10 
theintarray = 1,2,3,4,5 
thestringarray = cat, dog, bird 

我无法搞清楚如何获得提振programmagically负载theintarraythestringarray到像vectorlist的容器对象。我注定只是把它作为一个字符串读出来解析它自己吗?

谢谢!

回答

7

是的,你注定要自己解析。但它是相对容易的可能:

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")); 
+0

我是新来提高......但只有不拆分处理字符串?所以我不能用这个函数来填充'vector `? – 2011-02-13 22:44:08

相关问题