C / C ++开关盒带字符串 [英] C/C++ switch case with string

查看:82
本文介绍了C / C ++开关盒带字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C / C ++:切换非整数


我需要在开关情况下使用一个字符串。我的解决方案到目前为止是用我的哈希函数计算字符串的哈希。问题是我必须手动预先计算字符串的所有哈希值。有更好的方法吗?

Hi, I need to use a string in switch case. My solution so far was to calculate the hash of the string with my hash function. Problem is I have to manually pre-calculate all my hash values for strings. Is there a better approach?

h=_myhash (mystring);
switch (h)
{
case 66452:
   .......
case 1342537:
   ........
}


推荐答案

$ c> if(){} else if(){} chain。使用哈希值将是一个维护噩梦。 switch 旨在是一个低级语句,不适合字符串比较。

Just use a if() { } else if () { } chain. Using a hash value is going to be a maintenance nightmare. switch is intended to be a low-level statement which would not be appropriate for string comparisons.

这篇关于C / C ++开关盒带字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆