C++ 三维阵列到三维std::vector

C++ 三维阵列到三维std::vector,c++,arrays,vector,C++,Arrays,Vector,在我的代码函数中,我用3D std::vector替换了一个3D数组,它进入了一个无限循环。你能给我一个提示吗,我真的需要用一个向量来代替数组。谢谢:) 我最初的代码是: //arr is a 3D array of a sudoku table,the 3 rd dimension is for keeping values 0 to 13 //for a cell, and when I assign values I start from index 1 to 12 bool sol

在我的代码函数中,我用3D std::vector替换了一个3D数组,它进入了一个无限循环。你能给我一个提示吗,我真的需要用一个向量来代替数组。谢谢:)
我最初的代码是:

//arr is a 3D array of a sudoku table,the 3 rd dimension is for keeping values 0 to 13  
//for a cell, and when I assign values I start from index 1 to 12

bool sol(int arr[12][12][13]) {
int row,col;

if(!find_empty(arr,row,col)) return true;

for(int i=1;i< 12;i++) { //for digits 1 to 12
    if(is_working(arr,row,col,arr[row][col][i]) ) {   //if i can put the value in a cell
        arr[row][col][0] = arr[row][col][i];  //replace the first element for a cell with that value
     //here I want to use vector because I want to use an ac3 algorithm 
     //and remove those values that not satisfy constraints and shrink domain size having less values to verify with backtrack

        if(sol(arr)) return true;

        arr[row][col][0] = 0;
    }
}

return false;//if not backtrack
}
//arr是数独表的3D数组,第三维用于保持值0到13
//对于一个单元格,当我赋值时,我从索引1到12开始
布尔索尔(国际航空公司[12][12][13]){
int row,col;
如果(!find_empty(arr,row,col))返回true;
对于(int i=1;i<12;i++){//对于数字1到12
if(u正在工作(arr,row,col,arr[row][col][i]){//if我可以将值放入单元格中
arr[row][col][0]=arr[row][col][i];//用该值替换单元格的第一个元素
//这里我想使用向量,因为我想使用ac3算法
//并删除那些不满足约束条件的值,并缩小具有较少值的域大小,以便通过回溯进行验证
if(sol(arr))返回true;
arr[row][col][0]=0;
}
}
返回false;//如果不是回溯
}
我将arr替换为:

std::vector<std::vector<std::vector<int> > > vec;
vec.resize(12);
for(int i=0;i<12;i++)
{
vec[i].resize(12);
for(int j=0;j<12;j++)
{
    vec[i][j].resize(13);
    for(int k=0;k<13;k++)
        vec[i][j][k]=table[i][j][k];
   }
} 


bool sol(std::vector<std::vector<std::vector<int> > >& vec) {
int row,col;

if(!find_empty(vec,row,col)) return true;

for(int i=1;i< vec[row][col].size();i++) {//for remainig values in domain
    if(is_working(vec,row,col,vec[row][col][i]) ) {//same as above but having less values to verify for
        vec[row][col][0] = vec[row][col][i];

        if(sol(vec)) return true;

        vec[row][col][0] = 0;
    }
}

return false;
}
std::向量向量向量机;
向量调整(12);

对于(int i=0;i您的问题不够清楚。如果您也可以发布is_working的代码并发现_为空,那么我们将能够看到您是如何获得行和列的值的。
我本想把它作为一个评论,但作为一个新成员,又没有足够的声誉,我不得不把它作为一个答案。一旦你分享了is_working()和find_empty()的代码,我就会编辑它。我已经解决了这个问题。我使用了一个向量矩阵,而不是一个3D向量,它工作得很好:D

“编译时的无限循环”ehmmm这是什么意思?你确定要用索引
1
开始循环吗?
vector::resize
通过插入或删除元素来更改向量的实际内容。你是否尝试过调试并查看无限循环发生的位置?是的,它发生在中,因为问题不在代码中。问题是在数据结构方面,3D vector没有以某种方式修改其元素,导致算法不一致。这一定是代码或如何设置向量值的问题。但我很高兴您以某种方式使其正常工作。