通过使用分隔符C ++拆分文本来创建2D数组

时间:2013-09-19 18:02:09

标签: c++ arrays map

我有那段代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
std::string s = "0,0,0,1,1,1,0,0,1";
std::string delimiter = ",";

int x = 0;
std::string mapa[9];

size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
    token = s.substr(0, pos);
    std::cout << token << std::endl;
    s.erase(0, pos + delimiter.length());

    mapa[x] = token;
    x++;
}
std::cout << s << std::endl;
cin.get();
}

Parse (split) a string in C++ using string delimiter (standard C++)

我有x数组,但我需要第二个维度Y ...我从一个名为map.txt的文本文件中获取内容:

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

我需要用逗号分隔(对于x),然后用换行符(对于y)...

但Idk如何做Y阵......我该怎么办?

谢谢!

4 个答案:

答案 0 :(得分:1)

您可以将文件中的行读作

fstream fstr;
fstr.open("file.txt",ios::in);
string str;
int yDimension = 0;
while(getline(fstr,str)
{
    yDimension++;   //do appropriate thing with the y dimension
    std::string token;
    while ((pos = str.find(delimiter)) != std::string::npos) {
        token = str.substr(0, pos);
        std::cout << token << std::endl;
        str.erase(0, pos + delimiter.length());
        mapa[x] = token;
        x++;
    }
}

答案 1 :(得分:1)

您可以使用以下任意数量的逗号分隔列(内存允许)读取任意行数的整个文件:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>

struct int_reader : std::ctype<char>
{
    int_reader() : std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
        rc[','] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space;
        return &rc[0];
    }
};

int main()
{
    std::vector<std::vector<int> > myFileData;
    std::ifstream fin("MyDataFile.txt", std::ifstream::in);
    std::string buffer;
    while (std::getline(fin, buffer))
    {
        std::stringstream ss(buffer);
        std::vector<int> t;
            int_reader reader;
        ss.imbue(std::locale(std::locale(), &reader));
        std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(), std::back_inserter(t));
        myFileData.push_back(t);
    }
    // do whatever you need to with the loaded arrays ...
    return 0;
}

答案 2 :(得分:0)

你可以使用ifstream :: getline(),带分隔符','

ifstream file ( "map.txt" ); 
string value; 
while ( file.good() ) 
{
 getline ( file, value, ',' ); 
}

或者您可以阅读所有文本并使用正则表达式在分隔符之间取出每个文本。

答案 3 :(得分:0)

没有载体的最短路径:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

fstream fstr;
fstr.open("mapa.txt");

char mapa[31][9];
int x = 0, y = 0;
char c;
while(fstr.good())
{
    c = fstr.get();
    if (c!= ',') {
        mapa[x][y] = c;
        x++;
    }
    if (c=='\n')
    {
        x = 0;
        y++;
    }
}

fstr.close();
cin.get();

}

只有32行!! :d