Split string by delimeter

时间:2017-08-13 13:37:54

标签: c++ arrays string parsing

I have recently been trying to use TinyXML2 to read/write XML files, but I encountered a problem. I am trying to read a integer array that I exported from another program and it loads but TinyXML won't read integer arrays and I can't convert constant characters pointers to integers.

I want to separate comma separated values and store them in a array.

My code is as follows.

    int GetMapData (const char* XMLFile) {
        int mapdata[1];
        XMLDocument File;
        File.LoadFile(XMLFile);
        const char* data = File.FirstChildElement("map")->FirstChildElement("layer")->FirstChildElement("data")->GetText();
}

1 个答案:

答案 0 :(得分:1)

使用逗号更新

#include <sstream>

// ... 

char const *ss = "1, 2, 3, 4";  // this come from the FirstChildElement method in your case.
istringstream buffer(ss);
int value1, value2, value3, value4;
char c;
buffer >> value1 >> c >> value2 >> c >> value3 >> c >> value4;
cout << value1 << "-" << value2 << "-" << value3 << "-" << value4  << endl;

output: 
1-2-3-4
你正在寻找这样的东西吗?

当然你必须对它进行改进,但它应该给出这个想法。 的卡斯帕