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 '\n' 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, '\n'))
{
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));
}
}
}
I think you've confused the purpose of std::map
, a more efficient implementation might use something like std::vector
instead. Maps can be helpful when mapping pairs with non-integer keys, but for an application like this they're overkill.
#include <string>
#include <vector>
std::vector<std::vector<unsigned int>> parse_tiles(const std::string &gid_list) {
std::vector<std::vector<unsigned int>> tiles;
std::string value;
unsigned int row = 0;
for(unsigned int index = 0; index < gid_list.length(); index += 1) {
if(gid_list[index] == ',') {
tiles[row].push_back(std::stoi(value));
value.clear();
}
else if(gid_list[index] == '\n') {
tiles[row].push_back(std::stoi(value));
value.clear();
tiles.push_back();
row += 1;
}
else if(gid_list[index] >= '0' && gid_list[index] <= '9')
value += gid_list[index];
}
return tiles;
}
With this example you should be able to access elements in much the same way before. For example, out of context:
const auto tiles = parse_tiles(pData->GetText());
std::cout << "tile at (x = 2, y = 4) = " << tiles[4][2] << "\n";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.