C 中使用字符串的 switch 语句
我正在将字符串传递给函数 get_band(char *字符串) 然后我将传递的参数与特定字符串进行比较
if(strcmp(str, "auto"))
{
//do something
}
我的问题是 - 因为我有很多数量的字符串要比较,我如何使用 switch 语句而不是 if/else,因为 switch 语句仅支持整数类型。
Possible Duplicate:
C/C++: switch for non-integers
C/C++ switch case with string
I am passing a string to a function get_band(char *str)
Then I am comparing the passed argument with a specific string
if(strcmp(str, "auto"))
{
//do something
}
My question is - since i have many number of strings to compare, how can i use switch statement instead of if/else, since switch statement supports only integral type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简短的回答:你不能。
长答案:这个问题是C/C++ switch case with string的重复。
Short answer: you can't.
Long answer: this question is a duplicate of C/C++ switch case with string.
你可以这样做:
You could do:
不, switch 仅适用于整数。
如果要优化,可以使用一些数据结构来确定该字符串是否是任何已知字符串。例如:
要使用这样的数据结构,请为每个已知字符串分配一个常量整数(例如定义或枚举);给定一个字符串,您可以使用数据结构将其映射到已知字符串的某个数字(可能有一些数字表示“未知”)。 然后您可以使用此号码上的开关并针对每种情况执行您想要的操作。
您可以找到上述任何一个的现有实现,但如果您是 C 新手,那么自己实现它可能会有所帮助。
No, switch only works for integers.
If you want to optimize, you can use some data structure to determine if the string is any of the known strings. For example:
To make use of such a data structure, assign each known string a constant integer (e.g. define or enum); given a string, you can use the data structure to map it to some number of a known string (possibly with some number meaning "unknown"). Then you can use a switch on this number and do whatever you want for each case.
You can find an existing implementation of any of the above, but if you're new to C, it might be beneficial to implement it yourself.
switch 语句基于评估以下表达式进行分支。
在您的情况下, strcmp 仅承诺返回小于零、零或大于零。即使您假设这分别意味着 -1、0 和 1,这也意味着您在 switch case 中只能选择 3 个值。 [顺便说一句,这不是一个安全的假设...]
相反,如果您的字符串取自一组封闭的字符串(即只能有一组值),您可以对字符串进行标记。实际上有一些程序可以为您执行此操作,即将字符串转换为标记。 (请参阅 flex(1)。)这意味着您期望的每个字符串都将具有一些唯一的值,并且您可以打开该值。
如果这太麻烦,只需使用您在问题中使用的方法即可,即:
A switch statement branches based on evaluating the following expression.
In your case, strcmp only promises to return less than zero, zero, or greater than zero. Even if you assume that means -1, 0, and 1 (respectively), that would mean you would only have 3 values you could choose from in your switch cases. [And that is not a safe assumption, by the way ...]
Instead, if your strings are taken from a closed set of strings (i.e. can only have one of set of values), you could tokenize the string. There are actually programs to do this for you, i.e. turns strings into tokens. (See flex(1).) This would mean that each string you expected would have some unique value, and you could switch on that value.
If this is too much trouble, just use the method you used in your question, i.e.:
考虑更改程序的逻辑以使用枚举而不是字符串。然后,您可以使用数组查找将整数枚举映射到相应的字符串以用于显示和记录目的。其他选项包括哈希等都已涵盖。
Consider changing the logic of your program to use enums instead of strings. You can then use an array look up to map the integer enums to the corresponding string for display and logging purposes. The other options including hash etc have been covered.
C / C++ 中的 switch 语句仅接受三种情况类型:整数、字符和枚举数据类型(这实际上是整数的另一种表达方式)。当计算为 ascii 代码时,字符也会减少为整数。
@Nikolai N Fetissov 提出了一个很好的建议:打开字符串的哈希值:
The switch statement in C / C++ accepts only three case types : integers, characters and enumerated data types (which is effectively another way of saying integer). Characters also reduce to integers when evaluated as ascii codes.
@Nikolai N Fetissov made a good suggestion : switch on a hash of the string :