编译下面的代码会得到错误消息:type illegal。
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case"sda":
}
}
你不能在switch或case中使用字符串。为什么?是否有任何解决方案可以很好地支持类似于打开字符串的逻辑?
编译下面的代码会得到错误消息:type illegal。
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case"sda":
}
}
你不能在switch或case中使用字符串。为什么?是否有任何解决方案可以很好地支持类似于打开字符串的逻辑?
当前回答
Why not? You can use switch implementation with equivalent syntax and same semantics. The C language does not have objects and strings objects at all, but strings in C is null terminated strings referenced by pointer. The C++ language have possibility to make overload functions for objects comparision or checking objects equalities. As C as C++ is enough flexible to have such switch for strings for C language and for objects of any type that support comparaison or check equality for C++ language. And modern C++11 allow to have this switch implementation enough effective.
你的代码是这样的:
std::string name = "Alice";
std::string gender = "boy";
std::string role;
SWITCH(name)
CASE("Alice") FALL
CASE("Carol") gender = "girl"; FALL
CASE("Bob") FALL
CASE("Dave") role = "participant"; BREAK
CASE("Mallory") FALL
CASE("Trudy") role = "attacker"; BREAK
CASE("Peggy") gender = "girl"; FALL
CASE("Victor") role = "verifier"; BREAK
DEFAULT role = "other";
END
// the role will be: "participant"
// the gender will be: "girl"
可以使用更复杂的类型,例如std::pairs或任何支持相等操作(或快速模式的比较)的结构或类。
特性
支持比较或检验相等性的任何类型的数据 构建级联嵌套开关语句的可能性。 打破case语句的可能性 使用非常量大小写表达式的可能性 可以通过树搜索启用快速静态/动态模式(适用于c++ 11)
汉译语的差异在于语言的转换
大写的字 CASE语句需要括号 语句末尾不允许使用分号 冒号':' at CASE语句不被允许 在CASE语句的末尾需要一个BREAK或FALL关键字
对于c++ 97语言采用线性搜索。 对于c++ 11和更现代的可能使用快速模式wuth树搜索,其中CASE中的return语句变得不允许。 C语言实现中使用char*类型和以零结尾的字符串比较。
阅读更多关于这个开关实现的信息。
其他回答
hare对Nick解决方案的评论真的很酷。这里是完整的代码示例(c++ 11):
constexpr uint32_t hash(const std::string& s) noexcept
{
uint32_t hash = 5381;
for (const auto& c : s)
hash = ((hash << 5) + hash) + (unsigned char)c;
return hash;
}
constexpr inline uint32_t operator"" _(char const* p, size_t) { return hash(p); }
std::string s = "raj";
switch (hash(s)) {
case "sda"_:
// do_something();
break;
default:
break;
}
问题是,由于优化的原因,c++中的switch语句只能用于基本类型,你只能将它们与编译时常量进行比较。
这种限制的原因可能是编译器能够应用某种形式的优化,将代码编译为一个cmp指令和一个goto,其中地址是根据运行时参数的值计算的。由于分支和循环在现代cpu上不能很好地发挥作用,因此这可能是一个重要的优化。
要解决这个问题,恐怕你将不得不求助于if语句。
你不能在开关情况下使用字符串。只允许int和char类型。相反,您可以尝试用enum表示字符串,并在switch case块中使用它
enum MyString(raj,taj,aaj);
在switch case语句中使用它。
Why not? You can use switch implementation with equivalent syntax and same semantics. The C language does not have objects and strings objects at all, but strings in C is null terminated strings referenced by pointer. The C++ language have possibility to make overload functions for objects comparision or checking objects equalities. As C as C++ is enough flexible to have such switch for strings for C language and for objects of any type that support comparaison or check equality for C++ language. And modern C++11 allow to have this switch implementation enough effective.
你的代码是这样的:
std::string name = "Alice";
std::string gender = "boy";
std::string role;
SWITCH(name)
CASE("Alice") FALL
CASE("Carol") gender = "girl"; FALL
CASE("Bob") FALL
CASE("Dave") role = "participant"; BREAK
CASE("Mallory") FALL
CASE("Trudy") role = "attacker"; BREAK
CASE("Peggy") gender = "girl"; FALL
CASE("Victor") role = "verifier"; BREAK
DEFAULT role = "other";
END
// the role will be: "participant"
// the gender will be: "girl"
可以使用更复杂的类型,例如std::pairs或任何支持相等操作(或快速模式的比较)的结构或类。
特性
支持比较或检验相等性的任何类型的数据 构建级联嵌套开关语句的可能性。 打破case语句的可能性 使用非常量大小写表达式的可能性 可以通过树搜索启用快速静态/动态模式(适用于c++ 11)
汉译语的差异在于语言的转换
大写的字 CASE语句需要括号 语句末尾不允许使用分号 冒号':' at CASE语句不被允许 在CASE语句的末尾需要一个BREAK或FALL关键字
对于c++ 97语言采用线性搜索。 对于c++ 11和更现代的可能使用快速模式wuth树搜索,其中CASE中的return语句变得不允许。 C语言实现中使用char*类型和以零结尾的字符串比较。
阅读更多关于这个开关实现的信息。
C++
Constexpr哈希函数:
constexpr unsigned int hash(const char *s, int off = 0) {
return !s[off] ? 5381 : (hash(s, off+1)*33) ^ s[off];
}
switch( hash(str) ){
case hash("one") : // do something
case hash("two") : // do something
}
更新:
上面的例子是c++ 11。这里的constexpr函数必须是单语句。这在接下来的c++版本中得到了放宽。
在c++ 14和c++ 17中,你可以使用以下哈希函数:
constexpr uint32_t hash(const char* data, size_t const size) noexcept{
uint32_t hash = 5381;
for(const char *c = data; c < data + size; ++c)
hash = ((hash << 5) + hash) + (unsigned char) *c;
return hash;
}
c++ 17也有std::string_view,所以你可以用它来代替const char *。
在c++ 20中,您可以尝试使用consteval。