Boost property_tree - work with simple arrays or containers

I upload an ini file using boost property_tree. My ini file basically contains β€œsimple” types (ie Strings, ints, double, etc.), but I have some values ​​that represent an array.

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

I am having trouble figuring out how to get an impulse for a software download theintarrayand thestringarrayinto a container object like vectoror list. Am I really doomed to just read it as a string and parse it myself?

Thank!

+3
source share
1 answer

Yes, you are doomed to sort it out yourself. But this is relatively easy:

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

Source: https://habr.com/ru/post/1791859/


All Articles