我需要从标准输入中读取一个字符串,执行一些操作,同时我需要将一些输入复制到 char 数组中。 在 C++ 中,我可以这样做:
scanf("%s", &array[pos]);
将字符串 s
复制到 char array
的 pos
位置。
我需要非常快地完成这项工作(奥林匹克任务)。
在 C++ 中从一个 big 中读取需要 5 秒。
在 C# 上,我尝试在循环中使用 string.elementAt()
复制到数组,但它花费了 70 秒,这太多了。此外,构建一个大字符串而不是使用 string.ToCharArray() 是一个坏主意。
有什么想法吗?
char[] ciag = new char[1010001];
for(int x = 0 ; x < n ; x++){
line = Console.ReadLine();
sekw[x] = poz;
len = int.Parse(line.Split(' ')[0]); //length of string to copy
string znaki = line.Split(' ')[1]; //copied string
for (int j = 0; j < len; j++)
{
ciag[poz + j] = znaki[j]; //put into array. Perhaps slow.
}
poz += len;
ciag[poz++] = 'k'; //my stuff
}
最佳答案
public void CopyTo(
int sourceIndex,
char[] destination,
int destinationIndex,
int count
)
关于c# - 快速将字符串复制到字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863715/