C++中如何使用switch字符串

switch

 typedef std::uint64_t hash_t;

constexpr hash_t prime = 0x100000001B3ull;
constexpr hash_t basis = 0xCBF29CE484222325ull;

hash_t hash_(char const* str)
{
hash_t ret{ basis };

while (*str) {
ret ^= *str;
ret *= prime;
str++;
}

return ret;
}

constexpr hash_t hash_compile_time(char const* str, hash_t last_value = basis)
{
return *str ? hash_compile_time(str + 1, (*str ^ last_value) * prime) : last_value;
}

constexpr unsigned long long operator "" _hash(char const* p, size_t)
{
return hash_compile_time(p);
}
//--------------------------usage--------------------------//  
//oid simple_switch(char const* str)
//{
// using namespace std;
// switch (hash_(str)) {
// case "first"_hash:
// cout << "1st one" << endl;
// break;
// case "second"_hash:
// cout << "2nd one" << endl;
// break;
// case "third"_hash:
// cout << "3rd one" << endl;
// break;
// default:
// cout << "Default..." << endl;
// }
//}
//--------------------------usage--------------------------//  

 

原文链接: https://www.cnblogs.com/lobsterIT/p/5654145.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++中如何使用switch字符串

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/236609

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午5:05
下一篇 2023年2月13日 下午5:06

相关推荐