(C/C++) memset

本文对比了C语言使用memset函数和C#使用Array.Clear或Enumerable.Repeat进行字符串初始化的方法,提供了两种不同语言环境下字符串填充的实现方式。

C语言:

memset
   extern void *memset(void *buffer,int c,int count);
   #include <string.h>
   功能:把buffer所指内存区域的前count个字节设置成字符c
   说明:返回指向buffer的指针.

 

char a[100];
          memset(a,'\0',sizeof(a));

 

C#:

byte[]   test =   new   byte[65536];    

Array.Clear(test,0,test.Length);  

 

You could use Enumerable.Repeat:

byte[] a = Enumerable.Repeat((byte)10, 100).ToArray();

The first parameter is the element you want repeated, and the second parameter is the number of times to repeat it.

This is OK for small arrays but you should use the looping method if you are dealing with very large arrays and performance is a concern.

转载于:https://www.cnblogs.com/fdyang/p/4028314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值