Sunday, 18 November 2018

c++ - 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 list. Am I doomed to just read it in as a string and parse it out myself?




Thanks!


Answer



Yes you are doomed to parse on your own. But it's relatively easy possible:



template
std::vector to_array(const std::string& s)
{
std::vector result;
std::stringstream ss(s);
std::string item;

while(std::getline(ss, item, ',')) result.push_back(boost::lexical_cast(item));
return result;
}


which than can be used:



std::vector foo = 
to_array(pt.get("thestringarray"));


std::vector bar =
to_array(pt.get("theintarray"));

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...