Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

c++ - How to turn a string containing tile numbers into a map or vector that contains coordinates of the tilenumber?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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] == '
') {
            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] << "
";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...