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

Make 2D Array by Splitting a text with a delimiter C++

本文关键字:本来 数组 2D 文本来 分隔符 拆分 C++      更新时间:2023-10-16

好吧,我有那个代码:

#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();
}

使用字符串分隔符解析(拆分)C++字符串(标准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 数组...我能做什么?

谢谢!

您可以将文件中的行读取为

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++;
    }
}

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

#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;
}

您可以使用 ifstream::getline(),并带有分隔符 ','

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

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

没有向量的最排序方法:

#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