C++ 中的动态开关案例

发布于 2024-11-16 07:14:37 字数 347 浏览 2 评论 0原文

有没有办法可以用 C++ 构建动态 switch case。实际上,我仅在运行时才知道案例值,并且这些案例值的数量也仅在运行时才知道。我这样做的原因是我试图在运行时通过这个 switch case 构建一个完美的哈希查找。例如,我有四个值 89、94、38、54,我想要相当于以下内容的值。

switch( x )
{
case 89:
  return 0;
case 94:
  return 1;
case 38:
  return 2;
case 54;
  return 3;
}

但是,这应该在运行时构建。我们当然可以使用 for 循环和搜索来实现哈希查找,但这当然会比 switch case 慢。

Is there a way I can build a dynamic switch case in C++. Actually the case values are known to me only at runtime and the number of those case values are also known only at runtime. The reason I'm doing this is that I'm trying to build a perfect hash lookup through this switch case at runtime. So for example, i have four values 89, 94, 38, 54, I want equivalent of something as follow.

switch( x )
{
case 89:
  return 0;
case 94:
  return 1;
case 38:
  return 2;
case 54;
  return 3;
}

However, this should be built at runtime. We can of course use a for loop and search to implement hash look up, but that would be ofcourse slower than switch case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

浮华 2024-11-23 07:14:37

遗憾的是并非如此,switch case 仅接受编译时已知常量的值。当然,如果你只是使用 if 和 if else 而不是开关,它就能很好地满足你的要求。

Sadly not, switch cases only accept values that are known constants at compile-time. Of course, if you simply use if and if else instead of switches, it will do what you want just fine.

如梦初醒的夏天 2024-11-23 07:14:37

您可以使用哈希表来完成您正在寻找的事情。基本思想是将 89, 94, 38 , 54 映射到值 0, 1, 2, 3
查找将是

i = 38;
return hashtable[i];  //returns 2. 

如果最大值的上限很小(比如 99),那么

int hashtable[100];

:这只是一个数组。对于更复杂的场景,您可以使用哈希函数,或者最好使用预定义哈希表。

You can accomplish what you are looking for with a hash table. The basic idea is to map 89, 94, 38 , 54 to the values 0, 1, 2, 3.
The lookup will then be

i = 38;
return hashtable[i];  //returns 2. 

If you have a small upper limit on the largest value (say 99) this is just an array.

int hashtable[100];

For more complex scenarios you can use a hash function or preferably predefined hash table.

仅此而已 2024-11-23 07:14:37

您需要一个称为跳转表的东西 - 在您的情况下,在运行时构建。这基本上由一个返回所需值的函数对象数组组成,您可以在其中使用键对其进行索引。

您还可以使用哈希映射,如果范围相对较小,则效率较低,但如果范围较大,则效率肯定会更好。

You want something called a jump table- in your case, built at run-time. This basically consists of an array of function objects which return the desired values, where you index into it with the key.

You can also use a hash map, which is less efficient if the range is relatively small, but definitely better off if the range is large.

墨落成白 2024-11-23 07:14:37

不,您不能在运行时动态创建 switch case。
相反,您应该使用一些适合您需求的算法数据结构来表示您的哈希函数,并在其中进行查找。

No you cannot create a switch case dynamically at runtime.
Instead you should represent your hash function using some algorithmic data structure that suits your needs and make the look ups in that.

天涯沦落人 2024-11-23 07:14:37

我建议对数组进行二分搜索,或者如果您的域足够小,您可以只进行数组查找。

I suggest a binary search of an array, or if your domain is small enough you could just do an array lookup.

可是我不能没有你 2024-11-23 07:14:37

具有线性探测的 for 循环不是哈希表的示例。

标准做法是使用 boost、stl 和许多其他流行库提供的哈希类。它可能不会像开关那么快,但会比循环好得多。

A for loop with linear probing would not be an example of a hash table.

The standard thing to do would be to use the hash classes provided by boost, stl, and many other popular libraries. It will probably not be as fast as a switch, but will be far better than looping.

庆幸我还是我 2024-11-23 07:14:37

switch 语句通常在编译时优化为跳转表,因此这是不可能的。它们必须是编译时常量。 (可以使用模板元编程在编译阶段生成它们吗?)

switch statements normally are optimized to a jump table at compile time, so there is no way this could work. They have to be compile time constants. (Can you use template metaprogramming to generate them in the compile phase?)

花开半夏魅人心 2024-11-23 07:14:37

使用哈希图。如果有更快的方法,人们就会将其用作标准地图。

还要记住,过早的优化是万恶之源。

Use a hash map. If there were a faster method, people would use it as the standard map.

Also remember that premature optimisation is the root of all evil.

明媚殇 2024-11-23 07:14:37

如果你真的想要一个完美的哈希,我建议你要么使用 cmph 库,或者自己实现它(请参阅 本文 以使用 Python 实现的示例)

If you really want a perfect hash, I suggest you either use something like the cmph library, or implement it yourself (see this article for an example, implemented in python)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文