c++ - Boost property_tree - 使用简单的数组或容器

标签 c++ boost

我正在加载一个带有 boost property_tree 的 ini 文件。我的 ini 文件主要包含“简单”类型(即字符串、整数、 double 等),但我确实有一些值表示数组。

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

我无法弄清楚如何以编程方式将 theintarraythestringarray 加载到容器对象中,例如 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/

相关文章:

c++ - C++ 中的共享队列

c++ - Boost 测试因命名空间内的枚举类而失败

c++ - 在 Kubuntu 18.10 上链接的 Vulkan SDK 版本 1.1.85.0

c++ - 保留字典顺序的原始类型的字符串编码

c++ - 类 <template-parameter-1-2> 的默认参数的重新定义

c++ - Boost Enum 256 元素限制

C++ Boost 为两个不同的进程创建共享内存

c++ - 在 Boost 周围使用 "#pragma warning"时出现警告 C4503 包括

c++ - 在 QAbstractTableModel 上的 setModel 之后插入数据

C++:访问 shared_ptr 的容器应该返回原始还是共享 ptr?