开发者

Boost property_tree - working with simple arrays or containers

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

I'm having trouble figuring out how to get boost to programmagically load theintarray and thestringarray into a container object like vector or li开发者_运维百科st. Am I doomed to just read it in as a string and parse it out myself?

Thanks!


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;
}

which than can be used:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜