我正在加载一个带有 boost property_tree 的 ini 文件。我的 ini 文件主要包含“简单”类型(即字符串、整数、 double 等),但我确实有一些值表示数组。
[Example]
thestring = string
theint = 10
theintarray = 1,2,3,4,5
thestringarray = cat, dog, bird
我无法弄清楚如何以编程方式将 theintarray
和 thestringarray
加载到容器对象中,例如 vector
或 列表
。我注定只能将其作为字符串读入并自行解析吗?
谢谢!
最佳答案
是的,你注定要自己解析。但这相对容易:
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"));
关于c++ - Boost property_tree - 使用简单的数组或容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4986052/