首页 新闻 会员 周边 捐助

小弟是C++初学,如何把下面这段代码由C#翻译C++,谢谢。。。

0
[已关闭问题] 关闭于 2011-12-13 18:51
public class BloomFilter
  {
  private BitArray hashbits;
  private int numKeys;
  private int[] hashKeys;

  public BloomFilter(int tableSize, int nKeys)
  {
  numKeys = nKeys;
  hashKeys = new int[numKeys];
  hashbits = new BitArray(tableSize);
  }

  private int HashString(string s)
  {
  int hash = 0;
  for (int i = 0; i < s.Length; i++)
  {
  hash += s[i];
  hash += (hash << 10);
  hash ^= (hash >> 6);
  }
  hash += (hash << 3);
  hash ^= (hash >> 11);
  hash += (hash << 15);

  return hash;
  }

  private void CreateHashes(string str)
  {
  int hash1 = str.GetHashCode();
  int hash2 = HashString(str);

  hashKeys[0] = Math.Abs(hash1 % hashbits.Count);
  if (numKeys > 1)
  {
  for (int i = 1; i < numKeys; i++)
  {
  hashKeys[i] = Math.Abs((hash1 + (i * hash2))
  % hashbits.Count);
  }
  }
  }
  public bool Contains(string str)
  {
  CreateHashes(str);
  // Test each hash key. Return false if any 
  // one of the bits is not set.
  foreach (int hash in hashKeys)
  {
  if (!hashbits[hash])
  return false;
  }
  // All bits set. The item is there.
  return true;
  }
  public bool Add(string str)
  {
  // Initially assume that the item is in the table
  bool rslt = true;
  CreateHashes(str);
  foreach (int hash in hashKeys)
  {
  if (!hashbits[hash])
  {
  // One of the bits wasn't set, so show that
  // the item wasn't in the table, and set that bit.
  rslt = false;
  hashbits[hash] = true;
  }
  }
  return rslt;
  }
niesen111的主页 niesen111 | 初学一级 | 园豆:0
提问于:2011-03-17 12:42
< >
分享
所有回答(1)
0

把数据换一下就行了,基本的循环都是一样的,foreach换成 for 循环就可以了。但C++程序组织形式与C#有点区别,C++是在头文件里声明函数,在源文件里定义。C#中BitArray应该和C++中的bitset对应。

邢同举 | 园豆:220 (菜鸟二级) | 2011-04-08 19:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册