I am using tinyxml2 (https://leethomason.github.io/tinyxml2/index.html) to parse a .tmx/.xml file to C++. There is an ChildElement named data, it has text content.
<data encoding="csv">
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,
3,3,0,2,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,3,
3,3,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,3,
3,3,4,0,0,3,3,3,3,3,3,3,3,3,3,3,4,0,3,3,
3,3,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,
3,3,3,3,4,0,0,0,2,2,2,2,2,2,2,0,0,0,5,3,
3,3,3,3,3,4,0,6,6,6,0,0,0,0,0,0,0,5,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
</data>
I get the text via the tinyxml function GetText(). So each ',' is equal to one column -> x-coordinate, each '
' is limiting the line ->
y-coordinate. Im trying to get the Coordinates and the tilenumbers into a std::map, which only contains integer values (x,y,tilenumber). Until now my working solution looks like the code below. Now i am looking for a better solution. Currently i am only saving the line and i would get the x from the position in the vector.
XMLElement* pData = pLayer->FirstChildElement("data");
if (pData != NULL)
{
std::stringstream gidList;
gidList.str(pData->GetText());
std::vector<std::string> LineList;
std::map<int, std::vector<int>> gidDataList;
std::string gidLine;
while (std::getline(gidList, gidLine, '
'))
{
LineList.push_back(gidLine);
}
for (int line = 0; line < LineList.size(); line++)
{
std::string tilenumber;
std::stringstream LineListStream(LineList.at(line));
while (std::getline(LineListStream, tilenumber, ','))
{
gidDataList[line].push_back(std::stoi(tilenumber));
}
}
}
question from:
https://stackoverflow.com/questions/65889745/how-to-turn-a-string-containing-tile-numbers-into-a-map-or-vector-that-contains