通过使用分隔符C++拆分文本来制作 2D 数组
Make 2D Array by Splitting a text with a delimiter C++
好吧,我有那个代码:
#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
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '