将值分配给2D数组,而无需在C 中使用索引
Assigning values to 2d arrays without using index in c++
作为挑战,我不使用数组索引分配或搜索。我想做的是从文本文件中读取字符,然后将它们放入15x15的数组中。我使用索引创建了一个2D数组,但是没有它们就无法进行分配。我尝试了两件事,但我无法提出一个工作的想法。我的代码基本上是这样:
char **puzzleArray;
puzzleArray = new char*[15];
for (int i = 0; i < 15; i++)
puzzleArray[i] = new char[15];
char *c;
FILE* puzzle;
puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
fread(&c, sizeof(char), 1, puzzle);
if (*c != ' ' && *c != 'n')
strcpy(*(puzzleArray + (i * 15)) + j , c);
else
j--;
}
我也尝试过这样的分配:
char **puzzleArray;
puzzleArray = new char*[15];
for (int i = 0; i < 15; i++)
puzzleArray[i] = new char[15];
char c;
FILE* puzzle;
puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
fread(&c, sizeof(char), 1, puzzle);
if (c != ' ' && c != 'n')
*(puzzleArray + (i * 15)) + j = c;
else
j--;
}
但是它们都没有起作用,当我做第二个编译器时,我会给我错误的错误"作为分配的左操作数所需的lvalue"。
编辑:当我更改第二个代码为:`
*(*(puzzleArray + (i * 15)) + j)
它编译了,但是当我尝试使用以下方式打印数组中的字符时:
for (int i = 0; i < 15; i++) {
cout << endl;
for (int j = 0; j < 15; j++)
cout << *(*(puzzleArray + (i * 15)) + j);
}
//or using this
for (int i = 0; i < 15; i++) {
cout << endl;
for (int j = 0; j < 15; j++)
cout << puzzleArray[i][j];
}
它不会打印字符和程序崩溃。在Visual Studio中,它说一个例外发生在分配部分。
您不需要嵌套分配。更重要的是,您的代码不起作用,因为您假设嵌套分配将导致连续存储。
const int size = 15;
char* puzzleArray = new char[size*size];
// i'm not going to change file reading logic,
// but it should be done better
// ideally by one read to a buffer
// Also the style seems like a bad mix of C and C++,
// but I can't do much about it with only this little fragment of code.
FILE* puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < size; i++) {
for (int j = 0; j < size; j++) {
char c;
fread(&c, 1, 1, puzzle);
if (c != ' ' && c != 'n')
puzzleArray[i*size + j] = c;
/*
// this does not seem right, i don't know what you're trying to do here
// i'm gonna assume that you want to move to the next line
else
j--;
*/
else
break;
}
}
第二次尝试有两个问题。以及记忆是否连续不相关。您仍然可以使用2D数组并使用嵌套分配。(请注意,尽管有人向我的答案投下了投票,但我认为他/她做错了)。
这是使用2D数组和嵌套内存分配的全面测试的代码。
#include <iostream>
#include <cstdlib>
int main() {
using namespace std;
char **puzzleArray;
puzzleArray = new char*[15];
for (int i = 0; i < 15; i++)
puzzleArray[i] = new char[15];
char c;
FILE* puzzle;
puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < 15; i++) {
for (int j = 0; j < 15; ) {
fread(&c, sizeof(char), 1, puzzle);
if (c != ' ' && c != 'n') {
*(*(puzzleArray + i) + j) = c; // you can see the difference if you compare with your original code.
++j;
}
}
}
for (int i = 0; i < 15; ++i) {
for (int j = 0; j < 15; ++j) {
cout << puzzleArray[i][j]; // you could use pointer arithmatic here too. using index is just convenient to show the result.
}
cout << std::endl;
}
}
相关文章:
- 使用 std::vector 在类中分配索引
- 通过在编译时折叠带有索引的指针来分配 C 数组
- 在xarray中通过索引赋值会分配整个数组
- 在 c++ 中将数组的值分配给另一个数组的索引
- cpp 数组 - 分配常量索引有效,而非常量索引不起作用
- 如何在C++中为数组分配不同的索引值
- 是否可以将结构分配给向量的索引?
- 为字符串中的每个字符(不是字符位置!)分配唯一的索引
- 如何将文本文件的部分分配给数组的索引
- C++ 在类外部分配 char 数组的索引
- 将指针分配给字符串对象的第一个也是最后一个索引
- 如何将要分配给具有不同索引的单独变量分配给C 的单独变量
- 如何根据输入在数组中分配多个索引
- C 索引中的字符串映射而无需分配
- 提升多索引有序迭代器分配
- 具有相同索引的数组已更改,但未重新分配
- 如何在opencv中创建cv::matrix的向量,并将图像数据的子矩阵分配给向量的所有索引上的矩阵
- 将常量字符串分配给大小恒定的 char 数组,在未使用的数组索引中会发生什么
- 是否可以使用运算符重载按索引分配用户定义的数组?-C++
- 删除模板分配的数组,该数组可能已分配索引