c++中的memset在c#中如何替换?

qiufengily 2014-06-28 10:24:24
亲们 请问 c++中的memset在c#中如何替换?
...全文
774 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
久爱物联网 2014-08-05
  • 打赏
  • 举报
回复
public static void SetByte(Array array,int index,byte value) 参考:http://technet.microsoft.com/zh-cn/magazine/system.buffer.setbyte(VS.95).aspx Array.Clear(numbers1, 0, 50); 参考:http://msdn.microsoft.com/zh-cn/library/system.array.clear.aspx
qiufengily 2014-07-01
  • 打赏
  • 举报
回复
引用 4 楼 unearth 的回复:

 void memset(int[] dest, int c, int count)
        {
            for (int i = 0; i < dest.Length; i++)
            {
                dest[i] = c;
            }
        }

        void memset(bool[] dest, bool b, int count)
        {
            for (int i = 0; i < dest.Length; i++)
            {
                dest[i] = b;
            }
        }

这是我能想到的方法,或者你会有更好的方法,不过到时,请告诉我。
非常感谢!
qiufengily 2014-06-29
  • 打赏
  • 举报
回复
引用 2 楼 unearth 的回复:

            Buffer.SetByte()
亲 那请问这段代码应该怎么修改呢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 最大流
{
    class Program
    {
        static void Main(string[] args)
        {
            private const int maxN =201;

            private static int[,] edge = new int[maxN, maxN];
            private bool[] visited = new bool[maxN];
            private int[] father = new int[maxN];
            private int N; //边数,顶点数
            private int M;
            private int ans; //结果
            private void Ford_Fulkerson()
            {

            while(1)
            {
	            //一次大循环,找到一条可能的增广路径
	            Queue<int> q = new Queue<int>();
            //C++ TO C# CONVERTER TODO TASK: The memory management function 'memset' has no equivalent in C#:
	            memset(visited, 0, sizeof(visited));
            //C++ TO C# CONVERTER TODO TASK: The memory management function 'memset' has no equivalent in C#:
	            memset(father, -1, sizeof(father));
	            int now;
	            visited[0] = true;
	            q.Enqueue(0);
	            while(!q.Count == 0) //广度优先
	            {
		            now = q.Peek();
		            q.Dequeue();
		            if(now == M-1)
			            break;
		            for(int i = 0; i < M; i++)
		            {
			            //每次父亲节点都要更新,权值减为0的边就不算了.
			            if(edge[now, i] && !visited[i])
			            {
				            father[i] = now;
				            visited[i] = true;
				            q.Enqueue(i);
			            }
		            }
	            }
	            //可能的增广路不存在了
	            if(!visited[M-1])
		            break;
	            int u;
	            int min = 0xFFFF;
	            for(u = M-1; u; u = father[u]) //找出权值最小的边
	            {
		            if(edge[father[u], u] < min)
			            min = edge[father[u], u];
	            }
	            //减去最小权值
	            for(u = M-1; u; u = father[u])
	            {
		            //前向弧减去
		            edge[father[u], u] -= min;
		            //后向弧加上
		            //存在圆环,这句话关键
		            edge[u, father[u]] += min;
	            }
	            //当前增广路径增加的流
	            ans += min;
            }
            }

            private static int Main()
            {
            int s;
            int e;
            int w;
            while((N = SimulateCin.ReadToWhiteSpace(true)).Length > 0 && (M = SimulateCin.ReadToWhiteSpace(true)).Length > 0)
            {
	            ans = 0;
            //C++ TO C# CONVERTER TODO TASK: The memory management function 'memset' has no equivalent in C#:
	            memset(edge, 0, sizeof(edge));
	            for(int i = 0; i<N; i++)
	            {
		            s = int.Parse(ReadToWhiteSpace(true));
		            e = SimulateCin.ReadToWhiteSpace(true);
		            w = SimulateCin.ReadToWhiteSpace(true);
		            edge[s-1, e-1] += w;
	            }
	            Ford_Fulkerson();
	            Console.Write(ans);
	            Console.Write("\n");
            }
            return 0;
            }
           
            internal static class SimulateCin
            {
            private static bool goodlastread = false;
            internal static bool LastReadWasGood
            {
	            get
	            {
		            return goodlastread;
	            }
            }

            internal static string ReadToWhiteSpace(bool skipleadingwhitespace)
            {
	            string input = "";
	            char nextchar;
	            if (skipleadingwhitespace)
	            {
		            while (char.IsWhiteSpace(nextchar = (char)Console.Read()))
		            {
		            }
		            input += nextchar;
	            }
	            while ( ! char.IsWhiteSpace(nextchar = (char)Console.Read()))
	            {
		            input += nextchar;
	            }
	            goodlastread = input.Length > 0;
	            return input;
            }
            }
                }
            }
}
threenewbee 2014-06-29
  • 打赏
  • 举报
回复
C#不需要memset,因为结构体或者数据初始化都会被自动填充0。
孤独de猫 2014-06-29
  • 打赏
  • 举报
回复

 void memset(int[] dest, int c, int count)
        {
            for (int i = 0; i < dest.Length; i++)
            {
                dest[i] = c;
            }
        }

        void memset(bool[] dest, bool b, int count)
        {
            for (int i = 0; i < dest.Length; i++)
            {
                dest[i] = b;
            }
        }

这是我能想到的方法,或者你会有更好的方法,不过到时,请告诉我。
马少华 2014-06-28
  • 打赏
  • 举报
回复
Array.Clear()
孤独de猫 2014-06-28
  • 打赏
  • 举报
回复

            Buffer.SetByte()

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧