[英]Quickest way to copy a 2d array in C?
I have to code a program that inserts/deletes strings from a 2d array and is able to do Undo/Redo instructions as fast as possible.我必须编写一个从二维数组插入/删除字符串的程序,并且能够尽快执行撤消/重做指令。 I was thinking of creating a 2d array for each insertion/deletion instruction, so the Undo basically copies back the old 2d array into the current one.我正在考虑为每个插入/删除指令创建一个二维数组,因此撤消基本上是将旧的二维数组复制回当前的数组。
For example, my current 2d array is:例如,我当前的二维数组是:
"1: Hello “1:你好
2: World" 2:世界”
Then I ask to delete line #2, so now my current 2d array is: "1: Hello"然后我要求删除第 2 行,所以现在我当前的二维数组是:“1:你好”
and I want to create a new 2d array which contains:我想创建一个新的二维数组,其中包含:
"1: Hello “1:你好
2: World" 2:世界”
so that if I ask to Undo, it copies back "1: Hello 2:World" into the current 2d array.因此,如果我要求撤消,它会将“1:Hello 2:World”复制回当前的二维数组。
struct node{char** Instruction2dArray;
struct node* next;}
struct node* pointer;
i=0;
pointer->Instruction2dArray=malloc(max*sizeof(char*)) //max contains the number of written lines
while(CurrentArray[i][0]!='\0'){
pointer->Instruction2dArray[i]=malloc((strlen(CurrentArray[i])+1)*sizeof(char));
strcpy(pointer->Instruction2dArray[i],CurrentArray[i]);
i++;
}
I wrote this simple code to copy the current array, but I'm pretty sure this is the slowest way to do it and I don't know how to do it quicker.我写了这个简单的代码来复制当前数组,但我很确定这是最慢的方法,我不知道如何更快。 Thank you for your help.感谢您的帮助。
Since, You have said you want more Insertion/Deletion Operations, Linked Lists are more preferable.因为,你说过你想要更多的插入/删除操作,链接列表更可取。 If and Only You need more Random Access, prefer 2D string arrays.如果且仅当您需要更多随机访问,则更喜欢 2D 字符串数组。
Linked Lists are more optimized for Insertion/Deletion Operations, In My Opinion.在我看来,链表针对插入/删除操作进行了更优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.