Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用两个数组时的状态转换 我使用两个C++二维数组A1和A2。比如说,A1应该表示初始状态,A1=A2(就在第一轮中!)。接下来我想用A1保存旧状态,用A2保存机器的当前状态_C++ - Fatal编程技术网

使用两个数组时的状态转换 我使用两个C++二维数组A1和A2。比如说,A1应该表示初始状态,A1=A2(就在第一轮中!)。接下来我想用A1保存旧状态,用A2保存机器的当前状态

使用两个数组时的状态转换 我使用两个C++二维数组A1和A2。比如说,A1应该表示初始状态,A1=A2(就在第一轮中!)。接下来我想用A1保存旧状态,用A2保存机器的当前状态,c++,C++,我的问题是: -如何覆盖A2的值? -如何在完成一轮后将A1的状态覆盖为A2的状态 我以前的尝试:我只是使用了这样的值 #include <iostream> #include <stdio.h> using namespace std; class Machine { public: // attributes int x, j; int const constexpr static size = 2; int A1[size][size]; int

我的问题是: -如何覆盖A2的值? -如何在完成一轮后将A1的状态覆盖为A2的状态

我以前的尝试:我只是使用了这样的值

#include <iostream>
#include <stdio.h>

using namespace std; 


class Machine {

public:
// attributes
    int x, j;
int const constexpr static size = 2;
int A1[size][size];
int A2[size][size];
// functions

// creategrid: user sets the state at the beginning
auto creategrid() {
    for (x = 0; x < size; x++) {
        for (j = 0; j < size; j++) {
            std::cout << "1 or 0";
            printf("\n");
            std::cin >> A1[x][j];
        }
    }
}
    auto statetransition() {
    int a, m;
    for (a = 0; a < size; a++) {
        for (m = 0; m < size; m++) {
            if (A1[a][m] == 0) {
                A2[a][m] = 1;
            }
            A2[a][m] = 0;
            printf("%d ", A2[a][m]);
        }
    }
    printf("\n");
}
};
int main(){
Machine MA; 
MA.createfield();
MA.statetransition(); 
}
#包括
#包括
使用名称空间std;
类机器{
公众:
//属性
int x,j;
int const constexpr静态大小=2;
int A1[尺寸][尺寸];
int A2[尺寸][尺寸];
//功能
//creategrid:用户在开始时设置状态
自动创建网格(){
对于(x=0;xA1[x][j];
}
}
}
自动状态转换(){
int a,m;
对于(a=0;a

问题是,A2中没有应用任何更改

您可以使用以下方法将一个阵列复制到另一个阵列中:


虽然您的答案可能为OP的问题提供了解决方案,但它不能很好地解释为什么发布的代码示例不能按预期工作。您的实际代码比此处显示的代码少了两个大括号,这可以解释为什么会得到意外的结果。请注意,一个大括号是否存在可能会改变整个程序的含义。请阅读并确保此处显示的代码与您描述的行为相匹配。如果确实存在编译器错误,则需要将其包括在问题中[a][m]始终设置为0。我猜前面缺少了一个
else
,如果目标是将一个矩阵复制到另一个矩阵,使用向量或向量向量将简化您的生活看看
std::array
。它可能正是您想要的,为什么不执行std::swap而不是复制呢?
memcpy(A1, A2, sizeof(A1));