C# 快速将字符串复制到char数组
我需要从标准输入中读取字符串,执行一些操作,同时我需要将一些输入复制到char数组。 在C++中,我可以这样做:< /P>C# 快速将字符串复制到char数组,c#,string,input,C#,String,Input,我需要从标准输入中读取字符串,执行一些操作,同时我需要将一些输入复制到char数组。 在C++中,我可以这样做:< /P> scanf("%s", &array[pos]); 它将字符串s复制到位置pos处的字符array。 我需要尽快完成这项任务(奥运任务)。 阅读从一个大的时间在C++中花费了5秒。 在C#上,我尝试在循环中使用string.elementAt()复制到数组,但这花费了70秒,太多了。另外,构建一个大字符串并使用string.ToCharArray()是个坏主意。
scanf("%s", &array[pos]);
它将字符串s
复制到位置pos
处的字符array
。
我需要尽快完成这项任务(奥运任务)。
阅读从一个大的时间在C++中花费了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
}
char[]ciag=新字符[1010001];
对于(int x=0;x
试试:
char[] c = new char[stringa.Length];
for(int i = 0;i<stringa.Length;i++){
c[i] = stringa[i];
}
char[]c=新字符[stringa.Length];
对于(int i=0;i尝试:
char[]c=新字符[stringa.Length];
对于(int i=0;i使用以下公式:
using (Stream stdin = Console.OpenStandardInput())
{
byte[] buffer = new byte[1024];
stdin.Read(buffer, 0, buffer.Length);
char[] inputCharArray = System.Text.Encoding.Default.GetChars(buffer);
}
使用以下命令:
using (Stream stdin = Console.OpenStandardInput())
{
byte[] buffer = new byte[1024];
stdin.Read(buffer, 0, buffer.Length);
char[] inputCharArray = System.Text.Encoding.Default.GetChars(buffer);
}
使用StringBuilder
这段代码大约在一秒钟内处理100 x 10000个字符。
如果您需要更快的速度,可以使用命名空间System.Threading.tasks中的TPL-Task并行库使任务并行
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] input = CreateInput();
DateTime start = DateTime.Now;
for (int i = 1; i < 100; i++)
{
StringCutter(new String(input));
}
Console.WriteLine((DateTime.Now - start).ToString());
Console.ReadKey();
}
private static char[] CreateInput()
{
const int len = 10000;
char[] ret = new char[len];
for (int i = 0; i < len; i++)
{
ret[i] = Convert.ToChar(i % 256);
}
return ret;
}
private static String StringCutter(String str)
{
List<char> exclusion = new List<char> { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' };
StringBuilder result = new StringBuilder(str);
for (int i = 0; i < result.Length; i++)
{
if (!(exclusion.Contains(result[i])))
{
result.Remove(i, 1);
i--;
}
}
return result.ToString();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
char[]input=CreateInput();
DateTime start=DateTime.Now;
对于(int i=1;i<100;i++)
{
切弦机(新弦(输入));
}
WriteLine((DateTime.Now-start.ToString());
Console.ReadKey();
}
私有静态字符[]CreateInput()
{
常数int len=10000;
char[]ret=新字符[len];
对于(int i=0;i
使用StringBuilder
这段代码大约在一秒钟内处理100 x 10000个字符。
如果您需要更快的速度,可以使用命名空间System.Threading.tasks中的TPL-Task并行库使任务并行
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] input = CreateInput();
DateTime start = DateTime.Now;
for (int i = 1; i < 100; i++)
{
StringCutter(new String(input));
}
Console.WriteLine((DateTime.Now - start).ToString());
Console.ReadKey();
}
private static char[] CreateInput()
{
const int len = 10000;
char[] ret = new char[len];
for (int i = 0; i < len; i++)
{
ret[i] = Convert.ToChar(i % 256);
}
return ret;
}
private static String StringCutter(String str)
{
List<char> exclusion = new List<char> { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' };
StringBuilder result = new StringBuilder(str);
for (int i = 0; i < result.Length; i++)
{
if (!(exclusion.Contains(result[i])))
{
result.Remove(i, 1);
i--;
}
}
return result.ToString();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
char[]input=CreateInput();
DateTime start=DateTime.Now;
对于(int i=1;i<100;i++)
{
切弦机(新弦(输入));
}
WriteLine((DateTime.Now-start.ToString());
Console.ReadKey();
}
私有静态字符[]CreateInput()
{
常数int len=10000;
char[]ret=新字符[len];
对于(int i=0;i
用于(int x=0;x
用于(int x=0;x
你可以这样做
char[]Words=“example string test.tocharray();
char[]Words=Console.ReadLine().tocharray();
您可以这样做
char[]Words=“example string test.tocharray();
代码> CAR[]控制台。RealLoad().TochRARAYAL(;)/<代码> < /P>,要清楚,<代码> SCANF不是非常C++,它实际上是从C根继承的。在C++中,你会使用例如“代码> STD::CIN >和数组[POS ]。
。现在还不太清楚您在C端想做什么。抱歉,现在没有时间给出准确的答案,所以必须给出一个注释。您肯定想检查StringBuilder。字符串是不可变的,所以您无法更改它,这就是为什么您的操作需要这么长时间。您总是要重新创建一个新字符串。StringBuilder更适合高频NT对字符串的更改。你是否试过直接从流中读取它?有一些容易出错的错误会使这个速度慢下来。请把C++的代码放在后面。