[英]How to convert double pointer matrix to a single pointer vector in c++?
I have an double pointer matrix that i want to convert to a single pointer vector in c++. 我有一个双指针矩阵,我想将其转换为c ++中的单个指针向量。 here is the code:
这是代码:
int main(){
int AA[2][2] = {1,2,3,4};
int i,j,k;
int kolB,barB;
int **BB,**CC;
int *A,*B,*C;
BB = new int*[barB];
CC = new int*[2];
for(i=0;i<kolB;i++){
BB[i] = new int[kolB];
CC[i] = new int[kolB];
}
free(BB);
free(CC);
return 0;
}
i want to insert **AA value to *A, can you guys help me with this? 我想在* A中插入** AA值,你们可以帮我吗?
If i understand your question correctly, you want to access each element in the double pointer array and put it in a single pointer array like this: 如果我正确理解了您的问题,则希望访问双指针数组中的每个元素,并将其放在单个指针数组中,如下所示:
//assuming kolB,barB,**BB have been initialized
B=new int[kolB*barB];//create a one dimensional integer array to be pointed to by int* B
//then copy the value from double pointer array to this new one dimensional array
for(int i=0;i<barB;i++)
for(int j=0;j<kolB;j++)
B[(i*barB)+j]=BB[i][j];//you need to copy each value
If you are thinking of doing B=(int*)BB;
如果您正在考虑做
B=(int*)BB;
and access it like B[i]
, that will not work,so don't. 并像
B[i]
一样访问它,那是行不通的,所以不要。 BTW there are some interesting comments at the comment section of your question, you may want to take a look at them and improve your code. 顺便说一句,在您的问题的评论部分有一些有趣的评论,您可能希望看看它们并改善您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.