我有这个int向量,我必须从中保存值到2D数组。我认为这是直接的,但似乎对于i+j
,当j
变成0
时,下一个要保存的整数比上一个保存。他们重叠。你能告诉我怎么修理吗?下面是代码:
vector<int> temp_table;//filled it in previous code, just for info
int** arr_table =new int* [number_of_states];
for(int i = 0; i < number_of_states; i++)
{
arr_table[i] = new int[alphabet.size()];
}
for(int i=0;i<number_of_states;i++)
{
for(int j=0;j<alphabet.size();j++)
{
arr_table[i][j]=temp_table.at(i+j);//This is where the overlapping occurs
//when j=0.How to fix it to save the correct data?
}
}
for(int i=0;i<number_of_states;i++)
{
for(int j=0;j<alphabet.size();j++)
{
cout<<arr_table[i][j]<<" ";
}
cout<<endl;
}
您希望按alphabet.size()
缩放i
索引,以考虑已经添加的行:
for(int i=0;i<number_of_states;i++)
for(int j=0;j<alphabet.size();j++)
arr_table[i][j]=temp_table.at(i * alphabet.size() + j);