c++ switch/case 使用 string的方法

原文:http://blog.csdn.net/yozidream/article/details/22789147

有时候,我们想写出下面这样的switch语句:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const char* str = "first";  
  2. switch(str){  
  3. case "first":  
  4.     cout << "1st one" << endl;  
  5.     break;  
  6. case "second":  
  7.     cout << "2nd one" << endl;  
  8.     break;  
  9. case "third":  
  10.     cout << "3rd one" << endl;  
  11.     break;  
  12. default:  
  13.     cout << "Default..." << endl;  
  14. }  

但是在c++中,是不能用字符串来作为case的标签的;于是,很疑惑,我们只能用其他的办法来实现这样的需求。

但幸运的是,c++11引入了constexpr和自定义文字常量,将这两个新特性结合,我们实现出看上去像上面这样的代码。


基本思路为:

1、定义一个hash函数,计算出字符串的hash值,将字符串转换为1个整数;

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. typedef std::uint64_t hash_t;  
  2.    
  3. constexpr hash_t prime = 0x100000001B3ull;  
  4. constexpr hash_t basis = 0xCBF29CE484222325ull;  
  5.   
  6. hash_t hash_(char const* str)  
  7. {  
  8.     hash_t ret{basis};  
  9.    
  10.     while(*str){  
  11.         ret ^= *str;  
  12.         ret *= prime;  
  13.         str++;  
  14.     }  
  15.    
  16.     return ret;  
  17. }  

2、利用c++11自定义文字常量的语法,定义一个constexpr函数,switch的case标签处调用这个constexpr函数。如下

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. constexpr hash_t hash_compile_time(char const* str, hash_t last_value = basis)  
  2. {  
  3.     return *str ? hash_compile_time(str+1, (*str ^ last_value) * prime) : last_value;  
  4. }  


这个函数只有短短的一行,利用递归得到了与上面hash_函数得到的同样值,由于用constexpr声明了函数,因此编译器可以在编译期得出一个字符串的hash值,而这正是关键,既然是编译器就可以得到的整型常量,自然可以放到switch的case标签处了。

于是我们可以写出这样的swich语句:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void simple_switch2(char const* str)  
  2. {  
  3.     using namespace std;  
  4.     switch(hash_(str)){  
  5.     case hash_compile_time("first"):  
  6.         cout << "1st one" << endl;  
  7.         break;  
  8.     case hash_compile_time("second"):  
  9.         cout << "2nd one" << endl;  
  10.         break;  
  11.     case hash_compile_time("third"):  
  12.         cout << "3rd one" << endl;  
  13.         break;  
  14.     default:  
  15.         cout << "Default..." << endl;  
  16.     }  
  17. }  

这个实现中,hash_compile_time("first")是编译器计算出来的一个常量,因此可以用作case标签;而且如果出现了hash值冲突,编译器回给出错误提示。


3、上面的语法还不够漂亮,利用自定义文字常量,重载一个operator如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. constexpr unsigned long long operator "" _hash(char const* p, size_t)  
  2. {  
  3.     return hash_compile_time(p);  
  4. }  

现在我们可以写这样的文字常量,用“_hash”作为自定义文字常量的后缀,编译器看到这个后缀结尾的文字常量,就会去调用我们重载的operator,得到和调用hash_compile_time是一样的值,但看起来舒服多了:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. "first"_hash  


现在,我们写出的switch语句就好看多了。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void simple_switch(char const* str)  
  2. {  
  3.     using namespace std;  
  4.     switch(hash_(str)){  
  5.     case "first"_hash:  
  6.         cout << "1st one" << endl;  
  7.         break;  
  8.     case "second"_hash:  
  9.         cout << "2nd one" << endl;  
  10.         break;  
  11.     case "third"_hash:  
  12.         cout << "3rd one" << endl;  
  13.         break;  
  14.     default:  
  15.         cout << "Default..." << endl;  
  16.     }  
  17. }  
  18.    

全文完。希望我讲清楚了。
  • 40
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值