In my xml file I have arrays of ints written as follows: "1 10 -5 150 35", and I am using pugixml to parse it.
I know pugixml provides methods such as as_bool, or as_int, but does it provide an easy way of converting the string representation of an int array to a c++ object, or do I have to parse and separate the string myself? If so, any suggestions on how to do that?
A possibility would be to use std::istringstream
. Examples:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
{
std::istringstream in(std::string("1 10 -5 150 35"));
std::vector<int> my_ints;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(my_ints));
}
// Or:
{
int i;
std::istringstream in(std::string("1 10 -5 150 35"));
std::vector<int> my_ints;
while (in >> i) my_ints.push_back(i);
}
return 0;
}