当前位置:网站首页>How to use string branches for switch case
How to use string branches for switch case
2022-06-30 07:09:00 【HL_ Aeolus】
switch-case As stipulated in the grammar case You can't use a string case The branch , Sometimes there may be relevant requirements in the project
//common.h
#include <stdint.h>
using namespace std;
namespace common
{
constexpr uint32_t strToHash(const char* str);
uint32_t add(uint32_t a, uint32_t b);
};
constexpr uint32_t common::strToHash(const char* str)
{
uint32_t val(0);
for (int i = 0; str && ('\0' != str[i]); i++)
val = (val << 6) ^ (val >> 26) ^ str[i];
return val;
}
//main.cpp
#include <iostream>
#include <vector>
#include "common.h"
#include <string>
#include <stdio.h>
#define CASE(input,func) \ case common::strToHash(input): \ func; \ break;
void myDebug(string str)
{
cout << str << ",hash value :" << common::strToHash(str.c_str()) << endl;
}
void SwitchByString(string str)
{
if (str.size() == 0)
{
return;
}
const char* input = str.c_str(); //string convert to char *
switch (common::strToHash(input))
{
CASE("0x0002", myDebug("0x0002"))
default:
break;
}
}
int main()
{
SwitchByString("0x0002");
system("pause");
return 0;
}
边栏推荐
- 神经网络计算量及参数量
- 【已实现】服务器jar包启动脚本、shell脚本
- Vs2019 and SQL
- Definition and use of ROS topic messages
- [semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis
- Browser downloads files as attachments
- Go installation and configuration (1)
- Basic fragmentary thoughts
- sscanf 函数的使用
- Egret P2 physical engine (1) small ball falling demo
猜你喜欢
QT common macro definitions
Steps for formulating class or file templates in idea
Win10踩坑-开机0xc0000225
oracle数据库报列表中最大表达式为1000错误
B站首个UP主付费观看视频还是来了!价格“劝退”网友
Class template case - encapsulation of array classes
Install go language development tools
SOC_ AHB_ SD_ IF
2、 Layout system
Egret P2 pit encountered by physical engine (1)
随机推荐
SQL Server2005中SUM函数内嵌套IF语句
Unity中实现溶解(Dissolve)特效及其原理解析
Grep command usage
JS null judgment operators | and? Usage of
[JSON tutorial] Chapter 1 learning notes
B站首个UP主付费观看视频还是来了!价格“劝退”网友
Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集
What if I don't know what to do after graduating from university?
[Hot100]回文子串 与 最长回文子串
SOC_ SD_ CLK
How to convert XML to JSON
Performance comparison of random network, scale-free network, small world network and NS small world matlab simulation
Go语言指针介绍
app闪退
What underlying technologies support the metauniverse?
Mysql5.7 compressed version installation tutorial
对占用多字节和位的报文信号解析详解
免实名域名是什么意思?
[transfer] analysis of memory structure, cache and DMA architecture
Definition and use of ROS topic messages